Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a pointer to array, int (*ptr)[10], and how does it work?

int (*ptr)[10];

I was expecting ptr to be an array of pointers of 10 integers.

I'm not understanding how it is a pointer to an array of 10 integers.

like image 349
rahulvuppala v Avatar asked Sep 26 '20 11:09

rahulvuppala v


People also ask

What is the meaning of int (* ptr )[ 10?

int (*p)[10] means that p is now a pointer to an integer array of size 10. int *p[10] means that p is an array of 10 integer pointers . Follow this answer to receive notifications.

What is a pointer to an array?

CProgrammingServer Side Programming. Pointers are variables which stores the address of another variable. When we allocate memory to a variable, pointer points to the address of the variable. Unary operator ( * ) is used to declare a variable and it returns the address of the allocated memory.

What does the following declaration mean int (* ptr )[ 10 ]; a ptr is array of pointers to 10 integers B ptr is a pointer to an array of 10 integers?

int (*ptr)[10]; ptr is a pointer, but this declaration is called pointer of an array..

What does the declaration of the following mean int (* ptr )[ 5 ];?

Answer: (E) Explanation: Here p is basically a pointer to integer array of 5 integers. In case of “int *p[5]”, p is array of 5 pointers to integers.


4 Answers

ptr is of type "pointer to array of 10 int". In your declaration it is uninitialized so it doesn't point to any object. To make it point to an array:

int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

// initialize in the declaration:
int (*ptr) [10] = &arr;

// or after:
int (*ptr) [10];
ptr = &arr;
like image 140
bolov Avatar answered Nov 14 '22 23:11

bolov


I like to read it like this: (great answers already posted)

int (*ptr) [10];
     ^ a pointer
           ^ to an array of 10
^ ints

vs

int* ptr[10];
        ^ array of 10
   ^ pointer to int
like image 31
Andreas DM Avatar answered Nov 14 '22 23:11

Andreas DM


A pointer to an array of 10 ints is meant to point to arrays of 10 elements, no more, no less, or in case you have a 2D array, to point to a given row of such array, provided the it has exactly 10 columns.

An array of pointers to int, int *ptr[10], is just that, it has 10 pointers to int and to each one you can assing an address of an int, it can be part of an array or not.

Example 1:

int (*ptr)[10];

int arr[10];
ptr = &arr; //correct, arr has 10 elements

int arr2[12]; 
ptr = &arr2; //not correct, arr2 does not have 10 elements

Such pointer can be used to point to a 2D array with undetermined number of rows but a fixed number of columns.

Example 2:

int arr[5][10];
ptr = arr; //correct, pointer to the 1st row of a 2D array with 10 cols
ptr++; //now points to the second row        
    
int arr2[5][12]; 
ptr = arr2; //not correct, incompatible pointer, has too many cols

Example 3:

int(*ptr)[3];

int arr[2][3] = {{1, 2, 3}, {4, 5, 7}};
ptr = arr;

printf("%d", ptr[1][2]); //array indexing is identical as using arr[1][2] 

Array of pointer vs pointer to array

An advantage of pointer to array comes when it's time to dynamically allocate/free memory:

Example with a 2D array with 4 rows and 5 columns:

int (*ptr)[5]; 
ptr = calloc(5, sizeof *ptr); //simple assignment

free(ptr); //simple deallocation
int *ptr[5];

for (int i = 0; i < 5; i++)  //each pointer needs it's own allocation
    ptr[i] = calloc(5, sizeof **ptr);

for (int i = 0; i < 5; i++)  //each pointer needs to be freed
    free(ptr[i]);

On the other hand if you have an array of pointers you can have an uneven array, that is to say that the 1st row can have 10 ints but the 2nd can have 20:

int *ptr[5];

for (int i = 0; i < 5; i++)
    ptr[i] = calloc( i + 1, sizeof **ptr);

In the above example the 1st row of the 2D array has space for 1 int, the second for 2, the third for 3 and so on.

like image 45
anastaciu Avatar answered Nov 15 '22 00:11

anastaciu


A declaration gives a “picture” of how a variable will be used.1int (*ptr) [10] says “When (*ptr)[i] is used in an expression, it is an int.” From this, we can derive the type of ptr:

  • Since (*ptr)[i] is an int, (*ptr) must be an array of int.
  • Since (*ptr) is an array of int, *ptr must be an array of int.
  • Since *ptr is an array of int, ptr must be a pointer to an array of int.

Footnote

1 Kernighan and Ritchie, The C Programming Language, 1978, page 90.

like image 42
Eric Postpischil Avatar answered Nov 14 '22 23:11

Eric Postpischil