Q:

Function to remove leading whitespace does not change string in caller?

I have a function that needs to remove leading whitespace from a string. For some reason, this does not work:
static void remove_leading_spaces(char* line)
{  
   int i;
   for(i = 0; line[i] == ' '; i++);      //iterate through till whitespace     
   line = line + i;                      //  advance the pointer to point to
                                         //  the first non space character
}

For example, if I have a string with a single leading whitespace, the string will not be altered.
Thanks everyone the answers are correct for remove_leading_spaces!
How would I do this with a string that needs to be returned from the function that calls this function? I have been trying the same approach and I keep getting a segmentation fault? Is it the same exact concept?

A:

Parameters are passed by value in C. So even though you change the value of line here:
line = line + i;
the value of the pointer that you pass into remove_leading_spaces() doesn't change. If you want this to change the pointer in the caller, you'll need to pass the address of the pointer (which is how you pass by reference in C):
static void remove_leading_spaces(char** line)
{  
   int i;
   for(i = 0; (*line)[i] == ' '; i++);     
   *line = *line + i;
}

A:

In the function below you are passing the pointer by value.
static void remove_leading_spaces(char* line) 

Which means any change to line in the body is not reflected in the caller. You need to change your function like this:
static void remove_leading_spaces(char** line) 
{   
   int i; 
   for(i = 0; (*line)[i] == ' '; i++) { } // the ';' is a HUGE TRAP prefer { }
   *line += i;
} 

EDIT
Be careful NOT to free line after this as you would not deallocate the entire string. If the memory was dynamically allocated, you'll need to keep a backup copy of the original pointer to safely free it.

