0

If a variable is declared as:

char ** argv; 

And a function's definition is something like:

extern int my_system(const char *argv[]);

Now if we are passing the argument in the function as:

(my_system(argv))

then it throws the warning passing argument 1 of 'my_system' from incompatible pointer type.

What can be the possible solution to this?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Akshaya
  • 3
  • 1
  • 6
  • 1
    Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) which shows what you have tried. – Weather Vane Jun 24 '16 at 11:40

1 Answers1

0

In C, there is no implicit conversion between T** to const T**, because it would not prevent modification of ultimate element in all cases. See C FAQ's question 11.10 for enhanced explanation and example of "backdoor" modification.

In your case, gained understanding of intricacies, you could cast the pointer to silent the warning:

extern int my_system(const char *argv[]);

int main(int argc, char* argv[])
{
    my_system((const char**)argv);
}
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • Does that only cause a warning in the reverse situation where a `const` argument is passed to a non `const`? There is never a problem with `strcpy` etc. being passed a non `const` argument. – Weather Vane Jun 24 '16 at 11:50
  • @Weather Vane: The warning specified in question is about `char** --> const char**`. The reverse situation (passing `const char **` to function that expects parameter `char **`) is not allowed because of incompatible pointers, but it is another story. – Grzegorz Szpetkowski Jun 24 '16 at 11:53
  • 1
    You will get a warning in both directions, if you have two levels of indirection (`char **`). You will get a warning having a single indirection (`char *`) only, if you assign a const to a non-const. Therefore passing non-const to `strcpy()` et al. will not cause a warning. – Amin Negm-Awad Jun 24 '16 at 11:56
  • Hm.. I asked for the MCVE because MSVC does not complain about my supposed code based on the narrative. The dup linked starts by saying MSVC won't mind, but it complains for another reason (uninitialised var). – Weather Vane Jun 24 '16 at 12:03
  • @WeatherVane: MSVC is known to be not strictly conforming C implementation. The rules in C++ are different and I suspect that they just didn't bother to add that warning (of course I mean compiling in "C mode"). – Grzegorz Szpetkowski Jun 24 '16 at 12:17
  • @GrzegorzSzpetkowski if a function definition is of type **int execvp(const * file, char * const argv[])** and we define **ret = execvp(argv[0], argv);** then i can observe the warning: passing argument 2 of 'execvp' from incompatible pointer type. Can you tell the possible solution for this? – Akshaya Jun 24 '16 at 13:09
  • @AkshaySrivastava: How the `argv` argument is defined? If it is a `char* argv[]` from main, then it should work without warning. It is is `const char* argv[]`, the you would have to cast it back to form `char **`, so the `const` qualifired will be dropped. – Grzegorz Szpetkowski Jun 24 '16 at 14:27