How to remove first character from C-string?

Ash picture Ash · Nov 28, 2010 · Viewed 87k times · Source

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.

Answer

ruslik picture ruslik · Nov 28, 2010
if (contents[0] == '\n') 
    memmove(contents, contents+1, strlen(contents));

Or, if the pointer can be modified:

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