-5
#include<stdio.h>
int main(int argc,char *argv[]) {
    printf("%c",++**++argv);
    return 0;
}

Suppose the command line arguments passed were:

./a.out one two three

The output is:

p

Can someone please explain me what is happening?

user2736738
  • 30,591
  • 5
  • 42
  • 56

1 Answers1

6

Start from the back of the ++**++argv expression:

  • argv starts off as a pointer to element zero, i.e. "./a.out" or ""
  • ++argv is char** that points to string "one"
  • *++argv is char* that points to string "one"'s initial element
  • **++argv is char that is equal to string "one"s initial element, i.e. 'o'
  • ++**++argv is char that follows 'o'. On most systems that's 'p'.

The last operation modifies program's arguments in place, which is allowed by the standard (Q&A).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523