cstringcstring

How to remove first character from C-string?


Can anyone please help me? I need to remove the first character from a char * in C.

For example, char * contents contains a '\n' 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 help me with the code? I'm completely new to C, and just can't seem to figure it out.


Solution

  • if (contents[0] == '\n') 
        memmove(contents, contents+1, strlen(contents));
    

    Or, if the pointer can be modified:

    if (contents[0] == '\n') contents++;