c++ - When we call function and pass the character array, Why we call it by using character array name? -
i mean when define integer array this-
int main() { int a[100]={0,1,2,3}; cout<<a<<endl; return 0; }
it prints address of 0x69fe74 if pass array function this-
void fun(int *a) ///getting argument in integer pointer. right. { } int main() { int a[100]={0,1,2,3}; cout<<a<<endl; fun(a); ///function call putting name argument- return 0; }
but in character array when this-
int main() { char a[100]={'a','b'}; cout<<a<<endl; return 0; }
it prints elements of character array. output=ab question is- how can pass value function same thing character array how possible?
void fun(char *a) ///getting argument in character pointer. right. { } int main() { char a[100]={'a','b'}; cout<<a<<endl; fun(a); ///function call putting array name argument- return 0; }
you this
char s[100] = {'a', 'b'}; std:: cout << (void *) s;
std::cout overloaded 1 way char arrays (to print contents), way pointers (to print address). that's historical reasons. in modern c++ should pass std::string if wish print out series of characters.
Comments
Post a Comment