4

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));
  }
  • 1
    Assuming `argv` is the typical argument to `main`, then it should be `char**`, not `char*` and not `const char* const*`. – Cornstalks Jul 05 '16 at 03:27
  • And if you want to understand what this "monster" of `const char* const* argv` means, I highly recommend you read [this](http://c-faq.com/decl/spiral.anderson.html). As mentioned above, standard C mandates the second argument of `main` to be of type `char**`. – vsoftco Jul 05 '16 at 03:29
  • @Cornstalks Or `char* argv[]` would also work, just posting this for benefit of OP. – James Adkison Jul 05 '16 at 03:30
  • @JamesAdkison But that's just syntactic sugar for `char**`, isn't it? – vsoftco Jul 05 '16 at 03:31
  • @vsoftco Yes, it is. I was just pointing out the other syntax that may be encountered. – James Adkison Jul 05 '16 at 03:31

3 Answers3

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.

1

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.

Galik
  • 47,303
  • 4
  • 80
  • 117
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.

2785528
  • 5,438
  • 2
  • 18
  • 20