why we use "const char* const* argv" instead of "char *"? [duplicate]
Asked Answered
H

3

6

I'm working on simulating computer networks using "NS2 Simulator". I don't really understand why we should use const char* const* argv instead of char *?

Can I use char * instead of that? There are many QA about this subject but I've confused about that. Don't mark this question as "Duplicate", please . why we use const char* const* argv in the function below? is this a rule in c++ standard? can i use either string or char ** instead of that?

  Function Connector::command.
  //~ns/common/connector.cc
  int Connector::command(int argc, const char*const* argv)
  {
  Tcl& tcl = Tcl::instance();
  ...
  if (argc == 3) {
  if (strcmp(argv[1], "target") == 0) {
  ...
  target_ = (NsObject*)TclObject::lookup(argv[2]);
  ...
  }
  ...
  }
  return (NsObject::command(argc, argv));
  }
Hatchway answered 5/7, 2016 at 3:24 Comment(5)
Assuming argv is the typical argument to main, then it should be char**, not char* and not const char* const*.Latia
And if you want to understand what this "monster" of const char* const* argv means, I highly recommend you read this. As mentioned above, standard C mandates the second argument of main to be of type char**.Shopworn
@Latia Or char* argv[] would also work, just posting this for benefit of OP.Stamata
@JamesAdkison But that's just syntactic sugar for char**, isn't it?Shopworn
@Shopworn Yes, it is. I was just pointing out the other syntax that may be encountered.Stamata
M
4

const char*const* argv means "pointer to constant pointer to constant char". It's not the same as char*. There is a reason for the const modifier, as the argv pointer wont be reassigned, the elements will have to be accessed by subscript.

This makes it safe for the caller to dynamically allocate argv, pass it into command(), and free it later. Otherwise, if you point a pointer elsewhere before it is freed, then you've leaked the memory that it used to point to.

const char* const* argv creates two levels of indirection - first level is a const pointer to a const char, and second level is a pointer to the const pointer.

Mauney answered 5/7, 2016 at 4:59 Comment(0)
V
3

The programmer is allowed to const qualify arguments how they see fit. The benefit of this signature:

void func(const char* const* argv);

...is that it will accept argument arrays (of the type passed to main() or exec()), with any const qualification.

So all these are acceptable:

int main(int, char** argv)
{
    func(argv);
}

int main(int, const char** argv)
{
    func(argv);
}

int main(int, char* const* argv)
{
    func(argv);
}

int main(int, const char* const* argv)
{
    func(argv);
}

So if you are writing a function to accept argument array parameters (that your function will not modify) then its probably the best signature to select.

Volution answered 5/7, 2016 at 4:23 Comment(0)
P
0

why we should use "const ... argv" instead of "char *" ?

The 'const' is from you, the programmer, commanding the compiler to inform you when the code you write tries to modify argv.

can i use char * instead of that?

Possibly, and sometimes it does not matter. But if you mistakenly modify argv (or whatever the const var name is), then the compiler won't let you know you made a mistake, and the result may be something you do not want, even UB.

Principal answered 5/7, 2016 at 3:48 Comment(1)
Can you share a scenario where it can be UB?Glaciology

© 2022 - 2024 — McMap. All rights reserved.