As a newbie, I'm solving a problem from K&R's pointers chapter and was confused with some aspects of character pointers, and passing an array of type char* as a function parameter.
Char array decay I understand
Ex:
void main( )
{
char a[ ] = "Howdy";
tryMe(a);
}
so here function parameter passed is &a[0]
void tryMe( char* s )
{
printf("value I need: %c \n", *s);
}
So this would be s = &a[0];
Now using the above understanding, I tried to do the same for an array of type char*. with the same function tryMe(char*)
Ex::
char a[ ] = "Howdy";
char b[ ] = "Doodie";
char* a1 = a;
char* b1 = b;
char* AP[ ] = { a1 , b1 };
// now to pass array AP into the function tryMe::
tryMe(AP);
This is where I get a warning saying:
warning: passing argument 1 of ‘tryMe’ from incompatible pointer type [-Wincompatible-pointer-types]
Im not sure what is going on, but I did get it working fine by doing the following thing:
changing function defintion from
tryMe(char*) to tryMe(char**)
and this works fine.
The other solution that works is:
Function definition remains same::
void tryMe(char*)
but while passing the function parameter it goes as follows::
void tryMe( char* s[ ] )
{
;
}
Though these above changes work as I want them to, I'm lost as to why are they working
I would really appreciate any guidance on these.