In the following two lines in C:
int* a = (int *)calloc(automata_size, sizeof(int));
int* b = (a++);
I found that a and b share the same address. This is not the case if we have
int* a = (int *)calloc(automata_size, sizeof(int));
int* b = a + 1;
why?
So my advice is google post-increment and pre-increment. Pre-increment (
++a
) is:and is the same as:
Where as post-increment:
is the same as
this applies to pointers as well as integers.
The pre- and postfix
++
operators have a result and a side effect.The result of
a++
is the value ofa
prior to the increment. The side effect is thata
is incremented. Thus, ifa
is something like0x4000
andsizeof (int)
is 4, then after executingthe value of
b
will be0x4000
and the value ofa
will be0x4004
1.The result of
++a
is the value ofa
plus 1. The side effect is thata
is incremented. This time, the value of bothb
anda
will be0x4004
.Note: you will want to retain the original value returned from
calloc
somehow so that you can properlyfree
it later. If you pass the modified value ofa
tofree
, you will (most likely) get a runtime error.++
or adding 1 to a pointer advances it to point to the next object of the given type. On most systems in use today, anint
is 4 bytes wide, so using++
on a pointer adds 4 to the pointer value.