0

The following code will segfault.

char* input = "12.34"; //< this is only to simplify the example.

char buffer[30] = { 0 };
memcpy(buffer, input, strlen(input));

char* part1 = strsep(&buffer, "."); 

The following code will not.

char* input = "12.34"; //< this is only to simplify the example.

char buffer[30] = { 0 };
memcpy(buffer, input, strlen(input));

char* ptr = buffer; //< Only diff.
char* part1 = strsep(&ptr , "."); 

When passed by reference (&) as a function argument, why does the difference between char** and char*[30] matter?

gberthiaume
  • 91
  • 1
  • 1
  • 2
    The first type is not `char*[30]` but `char (*)[30]` (pointer to array, not array of pointers). And it is completely different from `char**` (pointer to pointer). – interjay Nov 13 '19 at 21:50

1 Answers1

0

If you look at the man for strsep, it needs the double pointer because it tries to assign the pointer.

"char *strsep(char **stringp, const char *delim);
...
and *stringp is updated to point past the token"

Basically if you have an array, you can't just tell the head of the array to be in a new place. You can however make a pointer to any element in that array and then change the value of the pointer (thereby making it point to a different element if you so wish).

Abel
  • 111
  • 4