3

While going through an example of pointers I came across a line which was *p[5] declares p as an array of 5 pointers while (*p)[5] declares p as a pointer to an array of five elements. I didn't understand the difference between them.

Could anyone explain it with an example?

Ahmad
  • 445
  • 5
  • 15
Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55

4 Answers4

9

1. int (*p)[5]

is called a Pointer to an Array(Array Pointer). We can declare a pointer that can point to whole array instead of only one element of the array.This pointer is useful when talking about multidimensional arrays.

In this example p is pointer that can point to an array of 5 integers.

/*Pointer to an array*/
#include<stdio.h>
int main(void)
{
    int *ptr; /*can point to an integer*/
    int (*p)[5];/*can point to an array of 5 integers*/
    int arr[5];
    ptr=arr;/*points to 0th element of arr*/
    p=&arr;/*points to the whole array arr*/
    printf("ptr=%p,p=%p\n",ptr,p);
    ptr++;
    p++;
    printf("ptr=%p,p=%p\n",ptr,p);
    return 0;
}
Output: 
ptr=0012FEAC,p=0012FEAC
ptr=0012FEB0,P=0012FEC0

Here ptr is pointer that points to 0th element of array arr,while p is pointer that points to the whole array arr.The base type of ptr is int while base type of p is ‘an array of 5 integers’.

We know that pointer arithmetic is performed relative to the base size,so if we write p++ then the pointer will be shifted forward by 20 bytes.

2. Now coming to *p[5]:

This is called Array of Pointers(Pointer Array)

Correct syntax is datatype *arrayname[size];

We can declare an array that contains pointers as its elements.Every element of this array is a pointer variable that can hold address of any variable of appropriate type.

/*Array of pointers*/
#include<stdio.h>
int main(void)
{
    int *p[5];
    int i,a=5,b=10,c=15;
    p[0]=&a;
    p[1]=&b;
    p[2]=&c;
    for(i=0;i<3;i++)
    {
        printf("p[%d]=%p\t",i,p[i]);
        printf("*p[%d]=%d\n",i,*p[i]);
    }
    return 0;
}
Output:
p[0]=0012FEB4   *p[0]=5
p[1]=0012FEA8   *p[1]=10
p[2]=0012FE9C   *p[2]=15

I hope this explanation along with examples will clear your concepts.

Hope this is helpful :)

Community
  • 1
  • 1
Ahmad
  • 445
  • 5
  • 15
1

The order of evaluation differs. *p[3] resolves to *(p[3]) but not (*p)[3]. Also note that *p is equivalent to p[0]. Therefore, ...

  • *p[3]*(p[3])(p[3])[0]p[3][0]
  • (*p)[3](p[0])[3]p[0][3]

Assuming you have an array of arrays, the difference is shown below:

visualisation

JojOatXGME
  • 3,023
  • 2
  • 25
  • 41
0

A pointer is like a sticky note; you write on it where you put the actual object.

An array is a box with equal compartments. It can store objects of the same type.

You have three balls.

An array of three pointers is a box that contains three stickers. Each sticker tells where each ball is located (they might not be all in the same place).

A pointer to an array of three elements is a sticky note that tells where to find the box. The box contains all tree balls.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

Let us see if this helps ,in short, assuming int

1) int *p[3]

 int *p1 = &i;
 int *p2 = &j;
 int *p3 = &k;

Instead of the above 3 separate pointers, I can have an array of pointers as int *p[3]. Each element in the array can point to i,j, and k, all are ints

2) int (*p)[3]

int array[3];

If I want a pointer which points to this array which have 3 elements, the pointer declaration will be int (*p)[3]. This is a pointer pointing to array

dlmeetei
  • 9,905
  • 3
  • 31
  • 38