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 char
s 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 char
s 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]) + " "))
}