Reputation: 31
This doubt is about pointers in 2-D array.
Here ptr
is a pointer and i
is representing row and j
is representing column.
I was taught in college that if I use (ptr+i)
then pointer will point to 1st element in i
th row. If I use *(ptr+i)
then pointer will print the first element in i
th row. If I use (*(ptr+i)+j)
then it is a pointer to j
th element in i
th row. If I use * ( *(ptr+i)+j))
then it refers to content available in i
th row and j
th column. But when I tried a program based on these it was in no way similar. I've written the outputs of the respective printf
s beside them. The last printf
was showing error.
void main()
{
int c[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int *ptr = c;
printf("\n%u", ptr); //713549104
printf("\npointing to 2nd row=%u", ptr + 2); //713549112
printf("\n1st element in 2nd row=%u", *(ptr + 2)); // 3
printf("\n3rd element in 2nd row=%u", (*(ptr + 2) + 3)); //OUTPUT 6
printf("\ncontent available in 2nd row, 3rd column=%u", *(*(ptr + 2) + 3)); //error
}
Is there some other way which we can use for pointers in 2-D array to point to a particular element in a column or row and print its value. Normal way I've understood that if I write (ptr+1)
then that should mean an increase by 4 bytes. But I don't understand why my sir wrote that it means increase in the row. And similarly other printf
s.
Upvotes: 0
Views: 233
Reputation: 530
#include <stdio.h>
int main()
{const int a = 3, b = 4;
int c[a][b] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int* ptr = *c;
printf("\n%u", ptr); //713549104
printf("\npointing to 2nd row=%u", ptr + 2); //713549112
printf("\n1st element in 2nd row=%u", *(ptr + b)); // 5
printf("\n3rd element in 2nd row=%u", (*(ptr + b) + 2)); //OUTPUT 7
printf("\ncontent available in 2nd row, 3rd column=%u \n", ptr[b + 2]); //7
for (int i = 0; i < a ; i++){
printf("\n");
for (int j = 0;j < b;j++)
printf("%2u ", *(ptr + i * b + j));
}
}
884761776
pointing to 2nd row=884761784
1st element in 2nd row=5
3rd element in 2nd row=7
content available in 2nd row, 3rd column=7
1 2 3 4
5 6 7 8
9 10 11 12
Upvotes: 0
Reputation: 11
Your first statement defines a 2D table, and populates it. You have not defined 1 or j. The ptr merely points to the beginning location of the table.
You need two pointers, one for the row, one for the column. In your example, c is defining a 3X4 table. You need to define two more int's (i and j) to be used to iterate through the table, initialize them then increment them as needed.
To iterate through the table element by element:
int i,j; // defines the two indices to the table
for(i=0; i==3; i++){ // walks through the three rows
for(j=0, j==4; j++){ // walks through the four columns
return (c[i][j]);
}
}
This would return in order 1 4 7 10
2 5 8 11
3 6 9 12
Modifying the 'for' statements will change the order of the return values.
Upvotes: 1
Reputation: 780788
Your basic assumptions are wrong. Since ptr
is declared a int *
, not int[4] *
, ptr + i
is not a pointer to the ith row, it's a pointer to the ith element in the array in row-major order.
To get a pointer to the jth element in the ith row, you need to use ptr + (i*4) + j
(and subtract 1 from each coordinate because array indexes are zero-based). And this whole thing needs to be dereferenced with *
:
printf("\n1st element in 2nd row=%u", *(ptr + (2*4) + 0));
printf("\n3rd element in 2nd row=%u", *(ptr + (2*4) + 2));
In your code, (*(ptr + 2) + 3)
simply adds 3
to the value found at the address ptr + 2
. + 3
is not applied to the address because it's outside the argument to the *
operator.
Upvotes: 1