I have been asked in an interview, there is an pointer to an array of 10 integers, something like this below.
int (*p)[10];
How do you allocate it dynamically ??
This is something I have done
p=(int *)malloc(10*sizeof(int));
But it looks wrong because I am not doing the right typecast.
So I would like to know what's the type of *p ??
Like int *p ,p is of type int.
Here is how to allocate:
p = malloc(sizeof *p);
or
p = malloc(sizeof (int [10]));
p
is of type int (*)[10]
and *p
is of type int [10]
.
p
is pointer to an array 10 of int
and *p
is an array 10 of int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With