user1446072
user1446072

Reputation: 93

difference of array related function arguments in C

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?

Upvotes: 0

Views: 198

Answers (6)

Jeyaram
Jeyaram

Reputation: 9474

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].

Upvotes: 1

Alexey Frunze
Alexey Frunze

Reputation: 62058

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

Upvotes: 3

wich
wich

Reputation: 17137

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

Upvotes: 0

Jim Diamond
Jim Diamond

Reputation: 1274

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)

Upvotes: 0

shan
shan

Reputation: 1202

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

Upvotes: 0

AGo
AGo

Reputation: 304

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.

Upvotes: 0

Related Questions