1

I know that If i define an array like

int a [10];

I can use a pointer notation, to access it's address using a+<corresponding_item_in_array> and it's value using, *(a+<corresponding_item_in_array>) .

Now I wanted to reverse things, I used malloc to allocate a memory to a integer pointer, and tried to represent the pointer in subscript notation but it didn't work

int *output_array;
output_array = (int *) (malloc(2*2*2*sizeof(int))); //i.e, space for 3d array

output_array[0][0][1] = 25;  
// ^ produces error: subscripted value is neither array nor pointer

I may have used an pointer expression using Storage Mapping, but ain't the simpler method available? and Why?

cipher
  • 2,414
  • 4
  • 30
  • 54
  • You can use the answer at this post to extend to 3rd dimension as well: http://stackoverflow.com/questions/1052818/create-a-pointer-to-two-dimensional-array – Tuxdude Feb 28 '13 at 01:55

3 Answers3

3

The int* type is not an equivalent of the 3D array type; it is an equivalent of a 1D array type:

int *output_array;
output_array = (int *) (malloc(8*sizeof(int))); //i.e, space for array of 8 ints
output_array[5] = 25; // This will work

The problem with arrays of higher ranks is that in order to index into a 2D, 3D, etc. array the compiler must know the size of each dimension except the first one in order to calculate the offsets from the indexes correctly. To deal with 3D arrays, define a 2D element, like this:

typedef int element2d[2][2];

Now you can do this:

element2d *output_array;
output_array = (element2d*) (malloc(2*sizeof(element2d))); 
output_array[0][0][1] = 25; // This will work now

Demo on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Does tha `malloc`ed, element be on the top, i mean, output_array[malloced][2][2] ? – cipher Feb 28 '13 at 02:10
  • 1
    Incidentally, you can do it even without the typedef, but the syntax is way more awkward (the declaration would be `int (*outputArray)[2][2];` and the cast would be `(int (*)[2][2])`). – Matteo Italia Feb 28 '13 at 02:18
  • @cipher Correct, it's on the inside. So if you do `malloc(200*sizeof(element2d))` the highest index would be `outputArray[199][1][1]`. – Sergey Kalinichenko Feb 28 '13 at 02:34
1

What's the type of output_array? int *.

What's the type of *(output_array+n) or output[n]? int.

Is subscript permitted on int? Both subscripts (eg. *(output_array+n) and output[n]) are pointer operations, and int is not a pointer. This explains the error you recieved.

You can declare a pointer to int[x][y] like this: int (*array)[x][y];

You can allocate storage that's a suitable alternative to a 3D array to array using: array = malloc(42 * x * y);. This would be the equivalent to int array[42][x][y];, except that arrays aren't modifiable lvalues, the alignof, sizeof and address-of operators work differently and the storage duration is different.

autistic
  • 1
  • 3
  • 35
  • 80
0

Because compiler doesn't have any idea about the size of each dimension, so it's not able to find out where output_array[0][0][1] should be.

You could try this

typedef int (* array3d)[2][2];
array3d output_array;
output_array = (array3d) malloc(2 * 2 * 2 * sizeof(int));
output_array[0][0][1] = 25;
printf("%d\n", output_array[0][0][1]);
neuront
  • 9,312
  • 5
  • 42
  • 71