Home:ALL Converter>const char * vs const char ** on argv

const char * vs const char ** on argv

Ask Time:2018-05-18T08:23:29         Author:Aureal

Json Formatter

I want to get a filename from a commandline argument so I can pass it to a file-opening function, however, argv[] seems to be of type const char ** by default, even if it was defined as "const char * argv[]" in the main's arguments. My function requires const char *, is there a way to convert this two, or something?

int main(int argc, const char *argv[]) {
    memory Memory;
    Memory.loadROM(argv); // Error 'argument of type "const char **" is incompatible with parameter of type "const char *"'
}

Author:Aureal,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50402143/const-char-vs-const-char-on-argv
iBug :

When used in function parameters without being a reference type, const char *argv[] and const char **argv is exactly identical because the former will be adjusted to the latter.\n\nPer N4296, Section 8.3.5p5:\n\n\n any parameter of type “array of T” or “function returning T” is\n adjusted to be “pointer to T” or “pointer to function returning T,” respectively\n\n\nSo const char *argv[] is \"an array of type const char *\" and is thus adjusted to \"a pointer to const char *\".\n\nAnd since argv is of type const char **, you want to dereference it to get a const char *:\n\nMemory.loadROM(argv[1])\n",
2018-05-18T01:03:45
scohe001 :

argv contains an array of char *, so if you only want one, simplify specify which you want:\n\nMemory.loadROM(argv[1]);\n\n\nRead here for more on what'll be in argv.",
2018-05-18T00:24:56
yy