dud3 dud3 - 1 month ago 16
C Question

Array of pointers evaluation

Can someone please tell me how would we evaluate the inner loop pointer:

*++argv[0]
.

How does it end up getting the second character from the argument?

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
while (--argc > 0 && (*++argv)[0] == '-') {

while ((c = *++argv[0])) {
switch (c) {
....
}
...
}
}
}

Answer

The array subscript operator has higher precedence than the prefix ++ and the dereference operator. The latter two operators have the same precedence and are evaluated from right to left. The expression in the second while loop is equivalent to:

*(++(argv[0]))

The pointer to the string is obtained using the array subscript operator, then it is incremented by one using prefix increment, it now points to the second character, and then the character is obtained using the dereference operator.