3

please excuse my bad english,

let me clarify, here is an example,

this is our main :

 main()
{
  int a1 []= {1,2,3,4,5,6,7,8,9} ;
  int size = sizeof(a1) /sizeof(a1[0]) ;
  point (a1 , size);

  return 0 ;
}

and this is the function:

void point(int a[] , int size)
{
  int i ;
  for (i = 0 ; i<size ; i++)
  printf("%d\n", a[i])) ;
}

From my knowledge, when an array is passed as an argument to a function, we are actually sending a pointer to the first element to the array..

With that being said, how come the function "point"'s parameter is an ARRAY variable, NOT a POINTER variable...?

The reason I thought this was weird, so for example in main we pass int* to some function :

  int* a = &b ;
  point2(a) ;

The function:

void point2 (int  a) // this would be invalid, it has to be int* a
{
  .
  .
}

We would have to specify that the function receives a pointer, How are arrays an exception?

PLEASE NOTE: I do understand that arrays decay to pointers; that's why my question never was "why can we send arrays as arguments, to functions that have pointers of that same type?". My question is, "How come even though arrays decay to POINTERS, its fine to keep the parameter of the function as an ARRAY?". Hope it is clear how this is different that the first question. Thank you!

jeff
  • 53
  • 1
  • 5
  • Please look at the note I have placed. I do not believe this is a duplicate – jeff Feb 23 '19 at 12:05
  • 1
    The compiler sees `void point(int *a, int size)`, so even if the original interface looks like it's taking an array, the compiler does not care. That's also why you see `int main(int argc, char*argv[])` and `int main(int argc, char**argv)`. – Bo R Feb 23 '19 at 12:27
  • Arrays can be implicitly converted to pointer to first item, at the same time when array is declared as an argument of function it will be adjusted to pointer. These are not the same thing! It is not possible to keep function parameters as arrays. – user7860670 Feb 23 '19 at 12:30

2 Answers2

6

The language specifications of both C and C++ state that a function parameter of type array of T is adjusted to type pointer to T. So these function declarations are one and the same:

void foo(int a[42]);
void foo(int a[]);
void foo(int* a);

All of these will accept a pointer parameter, be it the result of an array decay or not.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 6
    It should be noted that, although `void foo(int a[42])` has the same result as `void foo(int *a)`, this is not generally true for array declarations in functions. In C at least, the expression in brackets may be evaluated (the standard is silent on this), and some implementations do, so a function defined with `void foo(int a[printf("Hello, world.\n")])` may print ”Hello, world.” when is called. Further, constraints about array declarations apply before adjustment, so `void foo(int a[42][])` is not valid because the element type is incomplete, while `void foo(int (*a)[])` is valid. – Eric Postpischil Feb 23 '19 at 12:53
1

Syntactic sugar.

Like juanchopanza noticed, in function arguments it's equivalent to have an array or a pointer.

For fun, try this:

void foo(int a[42])
{
  printf("%zu\n", sizeof(a));
}

int main(int argc, char **argv)
{
  int a[42];
  printf("%zu\n", sizeof(a));
  foo(a);
}
juhist
  • 4,210
  • 16
  • 33