I read about (*ptr)[5] that it can point to a 5-element integer array. What this means?
-
Add the declaration of `ptr`. – Ratul Sharker Jan 20 '19 at 13:33
-
What part are you have trouble with specifically? – StoryTeller - Unslander Monica Jan 20 '19 at 13:35
-
can we simply use *ptr instead of (*ptr)[5] – V2K Jan 20 '19 at 13:39
-
Do you want to access the 5th item in the array pointer or are you dealing with a 2D array where one of the items is an array of 5 elements? – newkid Jan 20 '19 at 13:42
-
Can anyone explain the difference between (*ptr)[5] and *ptr if we use this pointer on array. – V2K Jan 20 '19 at 13:44
-
What's the difference between `int` and `int[5]`? One is an array, and one isn't. Same issue here. One is a pointer, one is an array of pointers. – William Pursell Jan 20 '19 at 14:11
-
You might want to read about pointer arithmetic. – alk Jan 20 '19 at 14:16
-
A nice picture in an answer to a similar question is shown here: https://stackoverflow.com/a/24104524/694576 – alk Jan 20 '19 at 14:22
2 Answers
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:
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"); }
}
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");
}
}

- 561
- 7
- 16
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.

- 60
- 6
-
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
-
2No, 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
-
-
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