0

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 ith row. If I use *(ptr+i) then pointer will print the first element in ith row. If I use (*(ptr+i)+j) then it is a pointer to jth element in ith row. If I use * ( *(ptr+i)+j)) then it refers to content available in ith row and jth column. But when I tried a program based on these it was in no way similar. I've written the outputs of the respective printfs 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 printfs.

3
  • stackoverflow.com/questions/11177410/… Commented Oct 31, 2017 at 21:42
  • Possible duplicate of 2D array and pointer in C - how to access elements? Commented Oct 31, 2017 at 21:43
  • There are more errors in your program. Some of them may be classified as "warnings" by your compiler but make no mistake, they are errors and you should treat them as such. You should generally fix them staring from the first one, because the last one may or may not be meaningful. In your particular case, ptr assignment is wrong and everything below it is meaningless. Commented Oct 31, 2017 at 21:57

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot sir. :)
1

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.

2 Comments

Ok, my formatting sucks. got to figure out how to use this editor I guess.
i==3 and j==4 is never true
0
#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 

IDEONE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.