#Command line arguments

30 messages · Page 1 of 1 (latest)

proud silo
#

I dont understand why the const* is written twice.
Also, compiler says, second argument of main should be char** why is that?
I've used char* argv[] but I dont understand this one

#include <iostream>
int main (int const argc, char const*const* argv) {
  for (int i = 0; i < argc; ++i) {
    std::cout << argv[i] << '\n';
  }
}

oak warrenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

errant stag
#

So first point, as a function argument char** argv and char* argv[] are equivalent

#

Second

#

!cdecl char const * const * argv

#

Damn, that command doesn't work :(

#

Basically it's saying that neither argv[n] or argv[n][m] can be changed

hasty warren
#

char const * const * means “pointer to const pointer to const char”

#

const char * const * is also the same

proud silo
#

🧐 “pointer to const pointer to const char”
looks a bit confusing

hasty warren
#

You read it from right to left

#

I know, it’s dumb

rain beacon
queen current
#

@proud silochar* argv[] = char** argv those are the same

rain beacon
#

^ not completely

queen current
#

nvm all the expert have already answer you

queen current
rain beacon
errant stag
#

As I understand it, they are the same for function arguments, and initializing from aleady existing arrays
But as a local array, they are different

const char* argv[] = {"Hello", "world"}; // Valid
const char* argv2[] = argv; // Invalid
const char** argv3 = argv; // Valid
rain beacon
#

but you can assign to double pointers

errant stag
#

My point being is that the types are different.
It's just that for function arguments, C just decides to treat them the same

proud silo
#

Okay now I kinda get it.
Thanks for the help guys 👍

#

!solved

oak warrenBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

proud silo
hasty warren
#

Yup, but there is a * in one of the comments which i assume is a typo?

#

This is a nice tool to clarify types

#

Made by @pseudo mauve