Reputation: 34884
I want to return more than one char* from a method:
char** test1() {
char **ret = malloc(sizeof(char*) * 3);
*ret = "aaa";
*(ret + 1) = "bbb";
// *(ret + 2) = "\0"; //needed?
return ret;
}
Do I need *(ret + 2) = "\0";
Or should it be *(ret + 2) = NULL;
?
Upvotes: 1
Views: 85
Reputation: 4076
As mentioned by Oli, You only have space for two elements, therefore you cannot do:
*(ret+2) = xxx;
as this is writing to that "third" element, which does not exist. You could allocate for three:
char **ret = malloc(sizeof(char*) * 3);
As to the answer to your question...
Do I need *(ret + 2) = "\0"; Or should it be *(ret + 2) = NULL;?
... given that you've allocated the right amount of space
then I would use...
*(ret+2) = NULL
... for this reason:
as NULL represents the value of a pointer as zero and "\0" does not, but in fact (is a literal that) points to some address which is not NULL, but contains a char that has the value zero.
Upvotes: 1