Reputation: 314
I've got an issue in understanding the difference in this two ways of incrementing pointers :
int **a;
a++; //works fine
**a++; //same here
a += n; //still good
**a += n; //is not the same and I can't figure out what is going on
I was trying to print parameters of the program in reverse order here
int main(int argc, char **argv)
{
argv += argc; //works fine until it is changed to
// **argv += argc
while (--argc > 0)
{
argv--;
(*argv)--;
while (*(*argv)++)
ft_putchar(**argv);
if (argc - 1 > 0)
ft_putchar('\n');
}
return (1);
}
Summing the question - why the second way is not working the same?
Upvotes: 1
Views: 59
Reputation: 75688
**a++
is parsed as **(a++)
while
**a += n
is parsed as (**a) += n
This is due to operator precedence
My advice is to always use parenthesis in cases like this to avoid any confusion.
Now on to each case:
a++
Pointer arithmetic. Post-increments a
**a++
is parsed as **(a++)
a
- Pointer arithmetic(a++)
is a
- the value before the incrementSo the above is equivalent (more or less) with the following:
**a;
a = a + 1;
a += n
Pointer arithmetic. I would expect self-explanatory.
**a += n
This is parsed as
(**a) += n
So you do a double indirection on a
getting the value of the pointed integer and then you increase that integer (integer arithmetic).
Upvotes: 7