Emil Mein Emil Mein - 24 days ago 6
C Question

Difference between ASturct arr[] and ASturct * arr[]

What is difference between ASturct arr[] and ASturct* arr[]?

ASturct array[]
ASturct * array[]


Thanks for answer!

Answer

You seem to have a basic misunderstanding about how arrays and pointers work.

Lets take a very simple example:

int a = 1;
int b = 2;
int c = 3;

int array1[3] = { a, b, c };

The above code creates an array of three int values. In memory it will look something like

+---+---+---+
| 1 | 2 | 3 |
+---+---+---+

The variables a, b and c are not anywhere in the array, the values of those variables were copied into the array when the array was initialized.

Now lets take a very different example:

int a = 1;
int b = 2;
int c = 3;

int *array2[3] = { &a, &b, &c };

Now we have an array of pointers, and initialize each element to point to the variables a, b and c. It will look something like this in memory

+----+----+----+
| &a | &b | &c |
+----+----+----+
 |    |    |
 |    |    v
 |    |    +---+
 |    |    | 3 |
 |    |    +---+
 |    v    
 |    +---+
 |    | 2 |
 |    +---+
 |
 v
 +---+
 | 1 |
 +---+

Both array1 and array2 in the example above are array with three elements, but the similarities end there. The type and value of each element in the arrays are very different from each other. For array1 each element is a single int, it holds a number (like e.g. 1 for array1[0]). For array2 each element is a pointer to an int value, and its value is the address of that int value.