-3

What is the difference between

int *arp = NULL;
int c;
++arp[c];

And

int *arp = NULL;
int c;
arp[c]; c++;

Please I need to know the meaning of this increment ++arp[c];

lena
  • 730
  • 2
  • 11
  • 23
  • `c` is uninitialized, so has an indeterminate value. Using it as an index on an array has undefined behavior. – Chris Jul 29 '23 at 22:12
  • arp is NULL and c has an unknown initial value. So it's undefined. But if that wasn't the case, then it would increase the value of whatever was inside arp at index c. – Irelia Jul 29 '23 at 22:19
  • What does the statement `arp[c];` do? This code is meaningless. – pmacfarlane Jul 29 '23 at 22:20
  • There is no difference: they are both undefined behaviour. But if you are asking the difference between `++arp[c];` and `arp[c]; c++;` or `arp[c++];` the first increments the array element and the second increments the array index. – Weather Vane Jul 29 '23 at 22:27

1 Answers1

3
++arp[c];

This dereferences arp[c] (same as *(arp + c)) and then uses the pre-increment operator to increase the value in arp[c] by 1. c remains unchanged.

arp[c];
c++;

This dereferences arp[c] but doesn't do anything with it so it's pointless. After that it uses the post-increment operator to increase c by 1. arp[c] remains unchanged.

Note: Both snippets have undefined behavior because arp points at NULL and c is unintialized when you read from it.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108