Jargon
Jargon

Reputation: 79

C expressions and their execution

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

Answers (2)

Gustavo Laureano
Gustavo Laureano

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

Paulo Bu
Paulo Bu

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

Related Questions