Issue
I have a warning when passing a pointer array (char**) as an argument of type void** in a function.
Below is my code. I use gcc compiler, it gives me this warning:
"warning: passing argument 1 of 'print' from incompatible pointer type [enabled by default]".
It points to this line print(lineptr, 2);
And another warning says:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'void *'.
And it points to this line printf("%s\n", *v++);
I tried replacing all void**
with char**
and it works well.
The reason that I want to use void**
is that I'm writing a quicksort function that can receive both character or integer arrays.
#include <stdio.h>
char *lineptr[100] = {"mister", "x"};
void print(void **, int n);
int main()
{
print(lineptr, 2);
return 0;
}
void print(void **v, int n)
{
while( n-- > 0 )
printf("%s\n", *v++);
}
The question is: does this warnings affect anything?
Now, I'm having no problems running the code and getting a desired output, but I asks if this method is guaranteed to work well every time.
Solution
You could cast the char
array to void
:
print((void **)lineptr, 2);
Generally it's best to pay attention to warnings and fix the issues to avoid undefined behavior.
Answered By - l'L'l
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.