0

For test purposes I have factored out my main code into do_something() and am now calling it from main. However this doesn't work as my char[][] args array is too stubborn to be converted to char**. This is what I'm trying:

        char args[1][15] = { { "www.google.com" } };
        char** argv = args;
        int argc = 2;
        do_something(argc, argv);

Yields:

char [1][15]" cannot be converted to "char **"

I don't know why it can't figure out how to decay the pointers? Should be obvious? Do I really need to reinterpret_cast this?

I've also tried this:

        char* args[] = { "www.google.com" };

But now the meaning is different: Here I don't initialize the subarrays with google, but I create an array of pointers to static string literals. Which makes my compiler complain:

const char [15]" cannot be converted to "char *"

How would any of you solve this problem?

EDIT: What I really want to achieve is to pass the char array to a main-like function. I can only do that if it satisfies char* argv[]. The question then is how do I get exactly this type from a non-const char[][]?

glades
  • 3,778
  • 1
  • 12
  • 34
  • The error is very clear: `args` is a `char[1][15]` array and it cannot be converted to `char**`. – Jason Oct 21 '22 at 05:29
  • 1
    *"Do I really need to `reinterpret_cast` this?"* -- only if you want to make your problem worse. – JaMiT Oct 21 '22 at 05:31
  • @JasonLiam Well certainly you can reinterpret the start address of this 2d array as char** somehow? Maybe I need the address of a pointer pointing to the start address? – glades Oct 21 '22 at 05:33
  • @glades You can make `argc` a `char*` as shown in my [answer](https://stackoverflow.com/a/74149166/12002570). See also [demo](https://onlinegdb.com/AOdHizPPR) – Jason Oct 21 '22 at 05:35
  • See also [Why does `int*[]` decay into `int**` but not `int[][]`?](https://stackoverflow.com/questions/14183546/why-does-int-decay-into-int-but-not-int) for the "why" (as opposed to the "how"). – JaMiT Oct 21 '22 at 05:36
  • *Note:* while `const char [15]` cannot be converted to `char*`, it can be converted to `const char*`. There's probably an existing question covering that somewhere. – JaMiT Oct 21 '22 at 05:39
  • @JaMiT The point is that I want to pass this array to a main-like function. I can only do that if it satisfies the condition `char* argv[]` So it cannot be const and it should be a double pointer. How do I do that? – glades Oct 21 '22 at 05:42
  • @glades I wasn't answering. I was not claiming that converting to `const char*` is something you should try, only that it could remove some of your confusion. As with my "see also", my "note" is aimed at the "why", while your question is about the "how". – JaMiT Oct 21 '22 at 05:44
  • When you're working with arrays of strings, don't use "C" style arrays. There are reasons C++ now has std::vector/std::array and std::string. One of them is : avoid memory leaks, the other clearer syntax on whether you want to pass data by value or (const) reference. vector and array also allow you to return arrays (without the extra documentation that the client is responsible for freeing memory) – Pepijn Kramer Oct 21 '22 at 05:54

0 Answers0