0

Currently I am learning C and trying to get my head around these instructions. Are they actually different?

++*argv

*argv++

*(++argv)

*(argv++)

Thanks!

AntikM
  • 636
  • 4
  • 13
  • 28
  • 1
    check the [operator precedence](http://en.cppreference.com/w/c/language/operator_precedence) table. – Sourav Ghosh Nov 13 '14 at 06:13
  • 3
    What don't you understand? Do you know what the pre and post increment operators do? Do you know about precedence? – David Heffernan Nov 13 '14 at 06:16
  • 1
    Do you know the different between `n++;` and `++n;`? Specify which part you don't understand. – Yu Hao Nov 13 '14 at 06:17
  • Yes. The problem is, ++ is at a higher precedence than * if I'm not mistaken. Then, calling ++*argv is decomposed into this: 1. increase argv, 2. point to it. But that's not the case. Why? – AntikM Nov 13 '14 at 06:19
  • What difference does the brackets make... why is ++*argv the same as *(argv++)? – AntikM Nov 13 '14 at 06:20
  • 1
    @antikbd No. This different precedence holds only in the `*x++` case. With `++*x`, the `++` clearly applies to `*x`. So `*` is executed first, and only then the `++`. – glglgl Nov 13 '14 at 06:39

4 Answers4

4

It's the postfix increment operator that has higher precedence than the pointer de-reference operator, not the prefix increment. So these two are equivalent:

*p++  *(p++)

The prefix increment has the same precedence as *, so *++p increments the pointer, and is the same as *(++p). Also, ++*p is the same as ++(*p).

Joni
  • 108,737
  • 14
  • 143
  • 193
2

Take a look at the below code.

main()
    {

       int a[4] = { 10,20,30,40};
       int *argv = a;
       t = ++*argv; 
       printf("%d\n",*argv); /* Here *argv is 11 */
        printf("%d\n",t); /* Here t is 11 because of pre-increment */

       *argv++; /* argv is incremented first ++ has higher priority over "*" */ 
       printf("%d\n",*argv);/* *argv is printed which is 20 */

       *(++argv); /* argv is incremented first ++ has higher priority over "*" */ 
       printf("%d\n",*argv); /* *argv is 30 */

       *(argv++); /* As explained above the same applies here also */
       printf("%d\n",*argv);
    }
Gopi
  • 19,784
  • 4
  • 24
  • 36
1

Two small examples of incrementing.
Tip: For a better understanding, try to imagine argc as 1 or 2.

Post-increment

In the program below, all arguments are printed including the program name argv[0].

int
main(int argc, char **argv)
{
    while (argc--)
        printf("%s\n", *argv++); /* same as *(argv++) */
}

Pre-increment

In the program below, all arguments are printed except the program name argv[0].

int
main(int argc, char **argv)
{
    while (--argc)
        printf("%s\n", *(++argv));
}

++*argv increments the value *argv.

psqli
  • 588
  • 6
  • 11
0

When you are using pre increment and post increment operators ie ++argv and argv++ in assignments you have to know about the rvalue and lvlue.Like whether the variable value will be incremented first and then get assigned to the LHS or after assigning to LHS the variable value gets incremented.Also the bracket changes the precedence.SO the concepts precedence,lvalue and r value,and rules of pointer increment needs to be understood.

achoora
  • 1,270
  • 1
  • 16
  • 32