int (*a)[5];
How can we Initialize a pointer to an array of 5 integers shown above.
Is the below expression correct ?
int (*a)[3]={11,2,3,5,6};
int (*a)[5];
How can we Initialize a pointer to an array of 5 integers shown above.
Is the below expression correct ?
int (*a)[3]={11,2,3,5,6};
Suppose you have an array of int of length
5
e.g.Then you can do
a = &x;
To access elements of array you:
(*a)[i]
(==(*(&x))[i]
==(*&x)[i]
==x[i]
) parenthesis needed because precedence of[]
operator is higher then*
. (one common mistake can be doing*a[i]
to access elements of array).Understand what you asked in question is an compilation time error:
It is not correct and a type mismatch too, because
{11,2,3,5,6}
can be assigned toint a[5];
and you are assigning toint (*a)[3]
.Additionally,
You can do something like for one dimensional:
Similarly, for two dimensional try this(thanks @caf):
{11,2,3,5,6}
is an initializer list, it is not an array, so you can't point at it. An array pointer needs to point at an array, that has a valid memory location. If the array is a named variable or just a chunk of allocated memory doesn't matter.It all boils down to the type of array you need. There are various ways to declare arrays in C, depending on purpose:
and then you access the content of the array as in: