C pointer notation compared to array notation: When passing to function

18,054

Solution 1

There is no real functional difference between the two notations. In C, when you pass an array variable to a function, it decays to a pointer regardless of the notation. However, in my opinion, the pointer notation is preferable. The problem with [] notation in function definitions is that, in my opinion, it is somewhat misleading:

void foo(int array[])
{

}

A ubiquitous mistake among novice C programmers is to assume that sizeof(array) will give you the number of elements in the array multiplied by sizeof(int), like it would if array were an array variable declared on the stack. But the reality is that array has been decayed to a pointer, despite the misleading [] notation, and so sizeof(array) is going to be sizeof(int*). array is really just a pointer to the first element, or possibly a pointer to a single integer allocated anywhere.

For example, we could call foo like this:

int x = 10;
foo(&x);

In which case the [] notation in the definition of foo is kind of misleading.

Solution 2

When you declare a function parameter as an array, the compiler automatically ignores the array size (if any) and converts it to a pointer. That is, this declaration:

int foo(char p[123]);

is 100% equivalent to:

int foo(char *p);

In fact, this isn't about notation but about the actual type:

typedef char array_t[42];
int foo(array_t p);  // still the same function

This has nothing to do with how you access p within the function. Furthermore, the [] operator is not "array notation". [] is a pointer operator:

a[b]

is 100% equivalent to:

*(a + b)

Solution 3

Those declarations are absolutely identical. To quote the standard:

A declaration of a parameter as "array of type" shall be adjusted to "qualified pointer to type"

C99 standard section 6.7.5.3 paragraph 7

Share:
18,054
SystemFun
Author by

SystemFun

Updated on June 10, 2022

Comments

  • SystemFun
    SystemFun almost 2 years

    My question is base on the following code:

    int myfunct(int ary[], int arysize)   
    int myfunct2(int *ary, int arysize)
    
     int main(void){
       int numary[10];
       myfunct(numary, 10)
       myfunct2(numary, 10)
       return;
     }
    
    int myfunct(int ary[], int arysize) {   
          //Whatever work is done
    
      }
    
    int myfunct2(int *ary, int arysize) {
         // Whatever work is done
    
      }
    

    Is there a reason to use one of these over the other? To elaborate, when concerned with numeric arrays, is there any reason one would want to use pointer notation over array notation. If one uses pointer notation then within the function pointer arithmetic would be used etc.. AND if one uses the [] array notation, one could work with the array as usual. I'm new to programming and I currently do not see any benefit to using the pointer notation.

    My precise question, is there any reason to pass a numeric array to a function using pointer notation and therefore using pointer manipulations within the function.

  • SystemFun
    SystemFun over 11 years
    I understand that. My question refers to the manipulations that will be made within that function.
  • SystemFun
    SystemFun over 11 years
    So if I use the *ary opposed to the ary[], can I use the ary[0] within the function?
  • melpomene
    melpomene over 11 years
    @Vlad If your question is about the insides of the function, why is your example code everything but that?
  • melpomene
    melpomene over 11 years
    @Vlad I thought you understood my answer? ary[0] is *(ary + 0) is *ary, regardless of context. If you can use one, you can use the other (in expressions, that is).
  • newacct
    newacct over 11 years
    "In C, when you pass an array variable to a function, it decays to a pointer regardless of the notation." No. An array decays to a pointer because the function takes a pointer parameter. "A ubiquitous mistake among novice C programmers is to assume that sizeof(array) will give you the number of elements in the array multiplied by sizeof(int)". No, if array were of an array type, then sizeof(array) ALWAYS gives you the number of elements times the size of the component type. But here array is not an array; it has pointer type (int *).
  • Charles Salvia
    Charles Salvia over 11 years
    What are talking about? "An array decays to a pointer because the function takes a pointer parameter" is the same thing as saying "when you pass an array variable to a function it decays to a pointer". A function can't very well take an array parameter now can it?
  • M.M
    M.M over 9 years
    Note that in the one-dimensional case, int array[size] has the same effect as int *array still, i.e. array is still a pointer, and the size is redundant except to serve as documentation. In the 2-D case the type of array is int (*)[size].
  • RobertS supports Monica Cellio
    RobertS supports Monica Cellio almost 4 years
    Your post is completely misplaced. The one thing hasn't anything to do with the other. That there is a difference between *arr and arr[] anywhere else in C except function parameter lists is obvious and not relevant to this question. It might even confuse somebody, who came here to get an answer for this notation at parameters. I'm on your side that the pointer notation is better, but it really hasn't anything to do with structure definitions in particular. -1
  • RobertS supports Monica Cellio
    RobertS supports Monica Cellio almost 4 years
    This post bolts down to that it doesn't make sense to use array notation when obviously want to pass pointers. But it is very confusing and has nothing to do with function definitions or linked lists in particular as you brought to focus.
  • RobertS supports Monica Cellio
    RobertS supports Monica Cellio almost 4 years
    We can use the pointer notation for passing pointers to multidimensional arrays too.
  • Neil
    Neil almost 4 years
    @RobertSsupportsMonicaCellio Well, maybe for the first dimension, but you still need array notation for the remaining dimensions anyway.