Can anyone please help me? I need to remove the first character from a const char *
in C.
For example, const char * contents
contains a 'x'
character as the first character in the array. I need to detect and eliminate this character, modifying the original variable after its been "sanitized".
Can anyone suggest how to achieve it? I'm completely new to C (though I know Java), and just can't seem to figure it out.
Note:
I already referred to these, and still not able to figure out:
How to remove first character from C-string? - this tell how to remove when the input is char * contents
AND
Difference between char* and const char*?
it mentions that const char* is a mutable pointer but points to immutable character/string
What I tried
below it works, but why it works?(it should not able to modify the immutable char array contents value "xup")
#include <stdio.h>
int main()
{
char input[4] = "xup";
removeLeadingX(input);
return 0;
}
int removeLeadingX(const char * contents)
{
//this if condition is something I have to add
//can't modify other code (ex: the method receives const char * only)
if(contents[0] == 'x'){
contents++;
}
printf(contents);//up - why immutable string "xup" updated to "up"?
return 0;
}