2

I'm having trouble understanding the usage of const here:

char* const args[]; 

does that mean args cannot point to a new address? How is it different from:

const char* args[];

Also I'm trying to traverse through this list and append the values to a string using a single for loop statement:

string t_command;
for(char** t= args; (*t) != NULL ; t++ && t_command.append(*t + " ")) {}

I'm not doing something right here and I can't figure out what.

5
  • There are approximately infinite questions about what const means in C/C++ here on Stack Overflow. Please use the search facility before posting... Mar 22, 2013 at 17:20
  • It means, array of const pointers, pointing to char. You cannot change them. Your loop is wrong. Mar 22, 2013 at 17:21
  • 4
    There's also http://cdecl.org to help you. Mar 22, 2013 at 17:21
  • Thanks for the comments. I understand char* const and const char* clearly. The question here has to do with double pointer consts.
    – ArmenB
    Mar 22, 2013 at 17:23
  • You should read pointers from right to left, in the first it is "constant pointer to a char" and in the second "pointer to a char that is constant"
    – Jona
    Mar 22, 2013 at 18:50

2 Answers 2

11
char* const args[]; 

args is an array. Each element in the array is a pointer to char. Those pointers are const. You cannot modify the elements of the array to point anywhere else. However, you can modify the chars they point at.

const char* args[]; 

args is still an array. Each element in the array is still a pointer to char. However, the pointers are not const. You can modify the elements of the array to point elsewhere. However, you cannot modify the chars they point at.

Diagram time:

Args:
┌───┬───┬───┬───┬───┬───┐
│   │   │   │   │   │   │  // In the first version, these pointers are const
└─╂─┴─╂─┴─╂─┴─╂─┴─╂─┴─╂─┘
  ┃   ┗━┓ ┗━┅ ┗━┅ ┗━┅ ┗━┅
  ▼     ▼
┌───┐ ┌───┐
│ c │ │ c │                // In the second version, these characters are const
└───┘ └───┘

Often, when you have a pointer to characters, those characters are part of an array themselves (a C-style string), in which case it looks like this:

Args:
┌───┬───┬───┬───┬───┬───┐
│   │   │   │   │   │   │  // In the first version, these pointers are const
└─╂─┴─╂─┴─╂─┴─╂─┴─╂─┴─╂─┘
  ┃   ┗━━━━━━━┓   ┗━┅ ┗━┅
  ▼           ▼
┌───┬───┬┄  ┌───┬───┬┄
│ c │ c │   │ c │ c │      // In the second version, these characters are const
└───┴───┴┄  └───┴───┴┄

As for traversing through the array, you are attempting to treat the args array as null-terminated. That's not how most arrays work. You should iterate using an index into the array.

Also note that you cannot add an array and a string literal together (as in *t ++ " "). Convert one side to a std::string to make it much easier.

So if N is the size of args:

for (size_t i = 0; i < N; i++) {
  t_command.append(std::string(args[i]) + " "))
}
0
2
char* const args[];

args is an array of constant pointers to char. The pointers can't be modified, but the things they point to can.

const char* args[];

args is an array of pointers to const char. The pointers can be modified, but the things they point to can't.

If you want a pointer to the first element of char* const args[];, then its type must be a pointer to a constant pointer:

for (char * const * t = args; *t; ++t) {
    t_command.append(*t + " ");
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.