Reputation: 79
I have a question about the C expression *cmd++='\0'
.
The program is as below:
void parse(char *cmd, char **arg)
{
while(*cmd!='\0')
{
while(*cmd==' ' || *cmd=='\t'||*cmd=='\n')
*cmd++='\0';
*arg++=cmd;
while(*cmd!='\0'&& *cmd!=' ' && *cmd!='\t' && *cmd!='\n')
cmd++;
}
*arg='\0';
}
Can you please explain it to me?
Upvotes: 3
Views: 107
Reputation: 566
Following the operator precedence http://en.cppreference.com/w/c/language/operator_precedence
the line
*cmd++='\0';
is equivalent to:
*cmd = '\0';
cmd++;
because cmd++ will return the original value and then increment, if you want to increment first, should be ++cmd
Upvotes: 2
Reputation: 29804
It assigns a value to what the pointer points to and increases the pointer after.
*cmd++='\0';
can be translated to:
*cmd='\0' // null char
cmd+=1
Upvotes: 2