I am confused about C syntax. If I allocate memory:
int* x = (int*)malloc(1 * sizeof(int));
Are these two code snippets of an int pointer equal?
*x = 0;
And
x[0] = 0;
I am confused about C syntax. If I allocate memory:
int* x = (int*)malloc(1 * sizeof(int));
Are these two code snippets of an int pointer equal?
*x = 0;
And
x[0] = 0;
The definition of the []
operator is: given ex1[ex2]
, it is guaranteed to be equivalent to
*((ex1) + (ex2))
Where ex1
and ex2
are expressions.
In your case x[0]
== *(x + 0)
== *(x)
== *x
.
See Do pointers support "array style indexing"? for details.