How must the prototype of this function look like to be compilable?
I have this code:
void PrintMainParameters(int n, char* array[])
{
int i = 0;
for(i = 0; i < n; i++)
{
printf("%s \n", array[i]);
}
}
int main(int argc, char* argv[] )
{
PrintMainParameters(argc, argv);
}
Works fine. Now I want to write PrintMainParameters as prototype to declare the function later in the source file.
I tried this one, but it says type mismatch, that the second parameter is an incompatible pointer type. I understand the compiler error, but I do not know why it occurs.
void PrintMainParameters(int, char*);
int main(int argc, char* argv[] )
{
PrintMainParameters(argc, argv);
}
void PrintMainParameters(int n, char* array[])
{
int i = 0;
for(i = 0;开发者_如何学运维 i < n; i++)
{
printf("%s \n", array[i]);
}
}
How must the prototype look like? Why does my code not work?
Your function takes an array of char pointers. Your prototype declares it to take a single char pointer instead. The correct prototype looks like this:
void PrintMainParameters(int, char*[]);
You can use either:
void PrintMainParameters(int, char**);
or:
void PrintMainParameters(int, char *[]);
Or if you prefer, you can insert a dummy parameter into the prototype, such as:
void PrintMainParameters(int argc, char *argv[]);
精彩评论