I was going through study materials from my previous year at University, and I saw a question like:
What is the difference between int *a
and int a[5]
and int *[5]
. What does the last one indicate?
I was going through study materials from my previous year at University, and I saw a question like:
What is the difference between int *a
and int a[5]
and int *[5]
. What does the last one indicate?
the int *a[5]
declares an array of pointers to int.
the easiest was to determine the specifics of a variable declaration is read it from right to left.
In a nutshell:
int *a
- creates a pointer to an int
. This should contain the memory address of another int
.
Example values of *a
are 0x00001
, 0x000057
, etc.
int a[5]
- creates an array that contains five int
elements with each element containing an int
values.
Here's a visualization of the possible values of each element in the array:
-------------------------
| Array element | Value |
-------------------------
| a[0] | 1 |
| a[1] | 2 |
| a[2] | 3 |
| a[3] | 4 |
| a[4] | 5 |
-------------------------
int *a[5]
- creates an array that contains five pointer to an int
elements which each element containing the memory address of another int
.
Here's a visualization of the possible values of each element in the pointer array:
-------------------------
| Array element | Value |
-------------------------
| a[0] | 0x000 |
| a[1] | 0x001 |
| a[2] | 0x002 |
| a[3] | 0x003 |
| a[4] | 0x004 |
-------------------------