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.