How to select an array pointer

I was asked in an interview, there is a pointer to an array of 10 integers, something like this below.

int (*p)[10]; 

How do you distribute it dynamically?

This is what I did

 p=(int *)malloc(10*sizeof(int)); 

But it doesn’t look right because I am not doing the right type.

So, I would like to know what type of * p ??

Like int * p, p is of type int.

+4
source share
1 answer

Here's how to highlight:

 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 a pointer to array 10 of int and *p is an array of 10 of int .

+9
source

Source: https://habr.com/ru/post/1392214/


All Articles