-1

I read about (*ptr)[5] that it can point to a 5-element integer array. What this means?

V2K
  • 23
  • 1
  • 10

2 Answers2

0

It can be used when you want to go through a 2-d or a higher dimensional array.

For example you have this 2-d array:

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

A normal *ptr will go through each of the elements in the array. If this array's base address is : 1000. Then the next address it will go to on increment would be 1002, 1004, 1006. Taking sizeof(int) => 2.

What (*ptr)[5] would do is to jump to the next 5th element and then point to it.

In the example taken above, if I want to jump on the very starting of each 1-d array in it, I would simply use (*q)[4] and jump to the next 4th element and just not the very next one.

So if you want to display the elements of this array you can do this in two ways:

  1. Using normal *ptr

    void display(int *q, int row, int col){

    int i, j;
    for(i=0; i<row; i++){
        for(j=0; j<col; j++){
            printf("%d ", *(q + i*col + j));
        }
        printf("\n");
    }
    

    }

  2. Using (*ptr)[4]

void show(int (*q)[4], int r, int col){

int i, j, *p;

for(i=0; i<r; i++){
    p = q+i;
    for(j=0; j<col; j++){
        printf("%d ", *(p+j));
    }
    printf("\n");
}

}

Shubham Yadav
  • 561
  • 7
  • 16
-1

int (*q)[5] means that q is a pointer to an array of 5 integers. To understand better let us use this pointer to an array of 5 integers.

void main()
{
 int a[][5] = {
               1,2,3,4,5,
               6,7,8,9,10,
               11,12,13,14,15
              };
 int *p;
 int (*q)[5];
 p = *a;

 printf("%d %d\n",p,q);
 p++;
 q++;
 printf("%d %d\n",p,q);
}

Output:

65500 65500

65502 65510

To begin with, both p and q contain the same address 65500. However, on incrementing p it points to an array of 5 integers.Hence on incrementing p it points to the next integer, whereas q starts pointing to the next 1-D array of 5 integers. Pointer to the array is very useful while passing a 2D array to functions.

  • 4
    `u` prints an `unsigned int`, to print a pointer's value cast the pointer to `void*` and use `p`, else undefined behaviour is invoked. – alk Jan 20 '19 at 14:13
  • because pointer always gonna return a positive value as locations are non-negative. So %u won't be a problem here. – Aashish Sharma Jan 20 '19 at 14:22
  • Although this "*pointer always gonna return a positive value as locations are non-negative*" might hold true for many platforms it does not necessarily do so in general. – alk Jan 20 '19 at 14:24
  • 1
    ... as an example this https://en.wikipedia.org/wiki/X86_memory_segmentation#Real_mode might serve. Pointer values here could be "*06EFh:1234h, 0812h:0004h, 0000h:8124h, etc.*" :) – alk Jan 20 '19 at 14:26
  • 2
    No, when a pointer is passed for `%u`, we cannot expect it will be treated as an `unsigned int`, for multiple reasons. If an eight-byte pointer is passed on stack where a four-byte `unsigned int` is expected, `printf` might pick up the more significant four bytes or the less significant. Or four bytes of padding might have been skipped to align the pointer, so `printf`, expecting an `unsigned int`, might pick up garbage from the padding. And some ABIs are complicated, sometimes passing things in registers, sometimes in memory. So `printf` might look in a different place altogether. – Eric Postpischil Jan 20 '19 at 14:31
  • ok, don't about this concept. Thank you for correction – Aashish Sharma Jan 20 '19 at 14:33
  • 3
    `p = (int*)a;` can be achieved without the pointer coercion with `p = &a[0][0];`, `p = a[0];` or `p = *a;`. – Eric Postpischil Jan 20 '19 at 14:34
  • @AashishSharma: You can edit your answer to correct or improve it, and then people might change their votes. – Eric Postpischil Jan 20 '19 at 17:50