2

I want to pass argv to another function, and can do it with no problems when I define the function like this:

void function(char** argv);

and call it from main with:

function(argv);

However, I would like to keep everything const where possible (I don't actually plan to change argv, or the value of either of the pointers). My problem is that as soon as I add the keyword const anywhere to argv in the function declaration I get conversion errors, e.g. this code

void function(const char** argv);

gives the compile error:

error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]

I've tried putting const in different places and get similar errors. Is there a way to pass argv while keeping the contents and pointers all constant?

Bugalugs Nash
  • 492
  • 1
  • 6
  • 21
  • 4
    Save yourself some headaches and store the parameters in a vector of strings and pass that instead. `std::vector arguments(argv + 1, argv + argc);` – Neil Kirk Aug 19 '14 at 15:02
  • With what arguments are you trying to call the function when you get the warning? With which types are they declared? – sth Aug 19 '14 at 15:03
  • @NeilKirk the function I'm passing to calls getopt which takes argv as an argument. If I make my own vector of strings I'd then have to convert it back to a char**, seems like more hassle than it's worth. – Bugalugs Nash Aug 19 '14 at 15:05
  • 1
    @BugalugsNash Do you have any variable of type `char* const*` in your code? If yes, please add the corresponding code. Also does [this answer](http://stackoverflow.com/a/14443518/509868) help? – anatolyg Aug 19 '14 at 15:08
  • @sth I've added that info in an edit – Bugalugs Nash Aug 19 '14 at 15:10
  • @anatolyg there is no `char* const*` in my code – Bugalugs Nash Aug 19 '14 at 15:14
  • 2
    Does the compiler say the error is in the call to function, and not something that function is calling? It kind of looks more like an error from within function. – dohashi Aug 19 '14 at 15:17
  • @dohashi you are right, the error I reported was due to the code within the function (calling getopt with argv). I should have reported the first error, which occurred at the point where the function was called, and still occurs when the entire body of the function is deleted: `error: invalid conversion from ‘char**’ to ‘const char**’ [-fpermissive]`. This may be due to the problem @anatolyg linked to. – Bugalugs Nash Aug 19 '14 at 15:33
  • @BugalugsNash getopt expects its argv to be `char * const argv[]`, so you making your argv `const char **` is incompatible. – dohashi Aug 19 '14 at 16:48
  • [Can the arguments of main's signature in C++ have the unsiged and const qualifiers?](https://stackoverflow.com/q/1621574/86967) – Brent Bradburn Feb 26 '18 at 00:15
  • [Why is main() argument argv of type char*[\] rather than const char*[\]?](https://stackoverflow.com/q/5808679/86967) – Brent Bradburn Feb 26 '18 at 00:26
  • [What is the proper declaration of main?](https://stackoverflow.com/q/4207134/86967) – Brent Bradburn Feb 26 '18 at 00:31
  • There are lots of near-dup's, but the simple answer to this question is: `int main( int argc, char * const argv[] )` – Brent Bradburn Feb 26 '18 at 00:32
  • I think you will benefit from seeing [this](https://stackoverflow.com/questions/2220916/why-isnt-it-legal-to-convert-pointer-to-pointer-to-non-const-to-a-pointer-to), it really helps me understand. – haxpor Jan 24 '21 at 22:33

0 Answers0