1

How can i declare a pointer to an array of 3 by 3 and use that pointer to print it.... The compiler is giving the error "[Error] subscripted value is neither array nor pointer nor vector "

#include <stdio.h>
int main(void)
{
    int A[3][3][3]={0};

    int *ptr=A;
    int i,j,k;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            for(k=0;k<3;k++)
            {
                printf("%d    ",*ptr[i][j][k] );
            }
            puts("");
        }
    }
    return 0;
}
4
  • 1
    That is not an array of 3 by 3 it's a 3x3x3 array which is usually not useful for anything. Commented May 23, 2015 at 16:43
  • int (*ptr)[3][3]=A; ... printf("%d ", ptr[i][j][k] ); Commented May 23, 2015 at 16:49
  • Don't use multidimensional arrays in C. Use only 1D arrays, then arr[i*width+j] etc... Commented May 23, 2015 at 17:03
  • My answer here shows how to encapsulate a multidimensional array into a struct which can be passed around. I also have a print function here. Commented May 26, 2015 at 4:45

3 Answers 3

3

The problem with your code is that you are declaring a pointer to an int, and casting an array to it. This is incorrect, because a pointer has one implicit "dimension" (and hence allows a single dereference), while your array has three dimensions.

To fix the problem, declare ptr as a pointer to a 3×3 array:

int (*ptr)[3][3]=A;

in which case you need to remove the dereference operator when accessing array elements

printf("%d    ", ptr[i][j][k]);

demo 1.

or a pointer to a 3×3×3 array

int (*ptr)[3][3][3]=&A;

in which case you need to dereference the pointer before applying indexes to it:

printf("%d    ", (*ptr)[i][j][k]);

demo 2.

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

Comments

2

Any multidimensional array is in fact a one dimensional array elements of which are in turn arrays.

This declaration

int A[3][3][3]={0};

can be rewritten the following way

typedef int T[3][3];

T A[3];

So you have an array A elements of which have type int[3][3] If you want to declare a pointer to the first element of the array then you have to write

T *ptr = A;

where ptr is a pointer to objects of type T. As T is an alias for int[3][3] then the preceding declaration can be rewritten like

int ( *ptr )[3][3] = A;

In this case the loops will look like

  for(i=0;i<3;i++)
  {
      for(j=0;j<3;j++)
      {
           for(k=0;k<3;k++)
           {
               printf("%d    ", ptr[i][j][k] );
           }

      puts("");

      }
  }

If you want to declare a pointer to the whole array itself then you can write

int ( *ptr )[3][3][3] = &A;

and the loops will look like

  for(i=0;i<3;i++)
  {
      for(j=0;j<3;j++)
      {
           for(k=0;k<3;k++)
           {
               printf("%d    ", ( *ptr )[i][j][k] );
           }

      puts("");

      }
  }

But I am sure that in your assignment you need to use a pointer to the first element of the array as I showed initially.

Comments

0

The problem is that accessing a pointer value with index notation works for a single index if the pointer is a single star pointer, like this

printf("%d\t", ptr[i + 3 * j + 9 * k]);

A three star pointer int ***ptr would allow the syntax ptr[i][j][k] but it would be wrong, because the pointer would point to a pointer of int pointers, which means that the data is arranged in a different way than that of the array int A[3][3][3], the array is more like a int *ptr pointer, because the integers are stored contigously.

In the int *** approach, it would be like an array of pointers to arrays of int pointers. Which means that ptr[i] would be equivalent to *(ptr + i) which is the same as *((char ***)ptr + sizeof(int **)), since sizeof(int **) is not necessarily the same as1 sizeof(int), then it would increment the pointer a different ammount than needed, leading to an obvious problem, the dereferenced pointer would not be pointing to the right place.

You could always declare pointers to arrays as the other answers mention, but I will skip that part, since it's in the other answers.


1It could be by coincidence the same.

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.