1

I am trying to point to an array of arrays (learning purposes). I use this line for that purpose:

int Arr[6][6];    
int (*ptr)[6][6];
ptr = &Arr;

Is this correct? (EDIT: I intend to use a pointer to a 6x6 array. Also, considering I wish to take input and print it out also, which kind of declaration is better? array of pointers or pointer to 6x6 array?)

Next,

(The "reading and printing of 2d array using pointer" code is a total mess, which is why I won't show it here.)

I want to use scanf and printf to take input through ptr (which should assign it to Arr), and then print both Arr and ptr to see the syntax (and if it works at all). Obviously both printed arrays should be the same.

What syntax should I use? It's extremely confusing and I hope this will help me learn better.

Reading and printing through pointer:

for (i = 0; i < 6; i++)

{
    printf_s("\nEnter values for row %d (Enter 0 for 1-1, 2-2 etc.:\n", i + 1);
    for (j = 0; j < 6; j++)
    {
        printf_s("Row %d Column %d", i + 1, j + 1);
        scanf_s("%d", &(*ptr)[i][j]); ........(ptr)[i][j] doesn't allocate to Arr correctly
    }
}
for (i = 0; i < 6; i++)
    {
        printf_s("\n");
        for (j = 0; j < 6; j++)
        {
            printf_s("%d\t", (*ptr)[i][j]); .........*(ptr)[i][j] is wrong because it dereferences the whole 2D array.
        }
    }

Credits: haccks (I changed the question a lot of times, so I'm pointing out mistakes I did during the same)

11
  • 3
    Why are we not allowed to see the declaration of Arr? Licensing issues? Commented Jan 3, 2014 at 22:31
  • 2
    array of pointers: T *arr[size];; array of pointers to array-of-arrays: T (*arr[size])[width][length]. Commented Jan 3, 2014 at 22:39
  • 1
    @user3138129; No..Arr is not just an int array. It is a 2D array of ints. Commented Jan 3, 2014 at 22:41
  • 1
    @H2CO3; Please don't do that. He will be confused for sure. Commented Jan 3, 2014 at 22:49
  • 2
    @haccks Yup. But that's what he asked for so... :P Commented Jan 3, 2014 at 22:57

1 Answer 1

3

int (*ptr)[6][6]; declare ptr as a pointer to an array of 6 elements , each of which are array of 6 integers ( of type int (*)[6][6] ) . Now &Arr is also int (*)[6][6] type, your assignment is legal.
In your scanf_s argument, you are missing &. This should be

scanf_s("%d", &(*ptr)[i][j]);  

Note that (*ptr)[i][j] is of int type. It is like Arr[i][j]. To store values to Arr[i][j] you need &, similarly & is used here to store values (*ptr)[i][j].

See the corrected program :

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int Arr[6][6];
    int (*ptr)[6][6];
    ptr = &Arr;

    for (int i = 0; i < 6; i++)
    {
        printf("\nEnter values for row %d (Enter 0 for 1-1, 2-2 etc.:\n", i + 1);
        for (int j = 0; j < 6; j++)
        {
            printf("Row %d Column %d: ", i + 1, j + 1);
            scanf("%d", &(*ptr)[i][j]);

        }
    }
    for (int i = 0; i < 6; i++)
    {
        for (int j = 0; j < 6; j++)
            printf("%d\t", (*ptr)[i][j]);
        printf("\n");
    }

}    

Output: http://ideone.com/q6BQ61

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

9 Comments

Technically, if Arr is also an array of arrays of arrays of integers, e.g. int Arr[X][6][6], that would be a legal assignment as well.
@JoachimPileborg; In that case I think Arr would be the legal assignment.
And how do I take input to ptr?
You can do this by (*ptr)[i][j].
okay, I'll show what I tried in another edit in the question. It didn't work though..
|

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.