void slice_first_char(char ** string)
{
*string = &(*string[1]);
}
int main(int argc, char * argv[])
{
char * input = "abc";
input = &(input[1]);
puts(input); // "bc" as expected.
slice_first_char(&input);
puts(input); // \372\277_\377
// What‘s going on?
}
How can I rewrite a slice_first_char function to make it work as expected?
You got the parentheses in
wrong. I guess you meant
Your original version dereferences the pointer of the first element of the
stringsarray, and then takes the address of that whereas you actually want to dereference the given pointer (because it just points to a single string), then take the first element of that (the first character) and then take the address of that.A slighty less convulated way to express this would be
by the way.