C expressions and their execution

I have a question about C expression *cmd++='\0'

.

The program looks like this:

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';
}

      

Could you explain this to me?

+3


source to share


2 answers


It assigns a value to what the pointer points to and increments the pointer after.

*cmd++='\0';

      



can be translated into:

*cmd='\0'  // null char
cmd+=1

      

+2


source


Following operator precedence http://en.cppreference.com/w/c/language/operator_precedence

line

 *cmd++='\0';

      



equivalent to:

*cmd = '\0';
cmd++;

      

because cmd ++ will return the original value and then increment it, if you want to increment it should be ++ cmd

+2


source







All Articles