0

I have defined an array:

float array[3][4][5];

then, what is the difference when

array, array[0], array[0][0], &array[0][0][0]

used as function argument?

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
user1446072
  • 83
  • 1
  • 8
  • 1
    possible duplicate of [Difference between passing array and array pointer into function in C](http://stackoverflow.com/questions/5573310/difference-between-passing-array-and-array-pointer-into-function-in-c) – wich Sep 12 '12 at 09:54

6 Answers6

3

The important thing to learn is that, in C, arrays aren't passed as parameters in their entirety. Instead, the pointer to the first element of the array is passed.

So, given the definition float array[3][4][5];...

array (as a parameter) will be of type float (*)[4][5], a pointer to a two-dimensional array of floats (explanation: we can't pass the array, we pass the pointer to its first element, which is a 4x5 array, hence float (*)[4][5]).

array[0] (as a parameter) will be of type float (*)[5], a pointer to a one-dimensional array of floats (explanation: array[0] is a 4x5 array, we can't pass the array, we pass the pointer to the first element of it, the first element being an array of 5 elements, hence float (*)[5]).

array[0][0] (as a parameter) will be of type float *, a pointer to a float (explanation: array[0][0] is an array of 5 elements, we can't pass the array, we pass the pointer to the first element of it, the first element being a float, hence float *).

&array[0][0][0] (as a parameter) will be of type float *, a pointer to a float (explanation: array[0][0][0] is a float, we pass a pointer to it, hence float *).

Perhaps, a more elaborate example:

#include <stdio.h>

int x[3][5] =
{
  {  1,  2,  3,  4,  5 },
  {  6,  7,  8,  9, 10 },
  { 11, 12, 13, 14, 15 }
};

int (*pArr35)[3][5] = &x;
// &x is a pointer to an array of 3 arrays of 5 ints.

int (*pArr5a)[5] = x;
// x decays from an array of arrays of 5 ints to
// a pointer to an array of 5 ints,
// x is a pointer to an array of 5 ints.

int (*pArr5b)[5] = &x[0];
// &x[0] is a pointer to 0th element of x,
// x[0] is an array of 5 ints,
// &x[0] is a pointer to an array of 5 ints.

int *pInta = x[0];
// x[0] is 0th element of x,
// x[0] is an array of 5 ints,
// x[0] decays from an array of 5 ints to
// a pointer to an int.

int *pIntb = *x;
// x decays from an array of arrays of 5 ints to
// a pointer to an array of 5 ints,
// x is a pointer to an array of 5 ints,
// *x is an array of 5 ints,
// *x decays from an array of 5 ints to
// a pointer to an int.

int *pIntc = &x[0][0];
// x[0][0] is 0th element of x[0],
// where x[0] is an array of 5 ints,
// x[0][0] is an int,
// &x[0][0] is a pointer to an int.

int main(void)
{
  printf("&x=%p x=%p &x[0]=%p x[0]=%p *x=%p &x[0][0]=%p\n",
         pArr35, pArr5a, pArr5b, pInta, pIntb, pIntc);

  return 0;
}

Output (ideone):

&x=0x804a040 x=0x804a040 &x[0]=0x804a040 x[0]=0x804a040 *x=0x804a040 &x[0][0]=0x804a040
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
1

All are points to same location.

#include <stdio.h>

    int main()
    {

    float array[3][4][5];
    printf("\n Address : %p, \n%p, \n%p, \n%p\n",array, array[0], array[0][0], &array[0][0][0]);
printf("\n Address : %p, \n%p, \n%p, \n%p\n",array+1, array[0]+1, array[0][0]+1, &array[0][0][0] + 1);

    return 0;
    }

gave me

    Address : 0x7fff51a2cac0, 
0x7fff51a2cac0, 
0x7fff51a2cac0, 
0x7fff51a2cac0

 Address : 0x7fff51a2cb10, 
0x7fff51a2cad4, 
0x7fff51a2cac4, 
0x7fff51a2cac4

The main difference comes when we increment the addresses.

array + 1       gives array[1][0][0]
array[0] + 1    gives array[0][1][0] 

then both array[0][0]+1 and &array[0][0][0] + 1 will points to array[0][0][1].
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
  • thank you very much, in fact, i also think they are the same addresses or pointers that point to the same place, but there must be some difference, because when i misuse them, compilation errors or something else come up – user1446072 Sep 12 '12 at 10:00
  • That answer doesn't tell all the differences. – Alexey Frunze Sep 12 '12 at 10:08
0

You're passing float[3][4][5], float[4][5], float[5], or float* in those cases, but all of them degenerate to float* when required.

See Difference between passing array and array pointer into function in C

Community
  • 1
  • 1
wich
  • 16,709
  • 6
  • 47
  • 72
0

There are no difference if you want to pass it into a function, because you can pass only pointer to array. But in function you can take as arguments sizes of array and calculate proper members by yourself.

AGo
  • 284
  • 2
  • 6
0

In terms of accessibility,

array = gives 3 dimensional array
array[0] = gives 2 dimensional array which is in array's 0th index
array[0][0] = gives 1 dimensional array which is in array's (0,0)th index
&array[0][0][0] = gives float *

if you says array[0][0][0], it gives just a float variable which is in first place

shan
  • 1,164
  • 4
  • 14
  • 30
  • You should probably elaborate this further since arrays aren't passed in their entirety, instead pointers to their element 0 are passed. – Alexey Frunze Sep 12 '12 at 10:26
0

As said, there is no difference.

For example, if you have an array

float x[10][10];

You can reference 3-d item int the first "row", using two different ways:

x[1][3]

or

*(*(x+1) + 3)

Xentatt
  • 1,264
  • 22
  • 35