views:

44

answers:

2

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?

+6  A: 

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*[]);
Thomas
Thanks, now I do understand my problem and its fixed ;)
citronas
+4  A: 

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[]);
Jerry Coffin
+1 for the last option, which greatly improves the readability for anyone looking at only the prototype.
Daniel Stutzbach