0

What is the difference between the below codes when passing the array to the function? Which should be used when writing a code?

PS: I would like to know the difference in using int *ptr vs int ptr[] in the function argument.

void foo(int *ptr)
{
    ...
    ...
}

void main()
{
   int a[]={10,20,30};
   foo(a);
   
}

vs

void foo(int ptr[])
{
    ...
    ...
}

void main()
{
   int a[]={10,20,30};
   foo(a);
   
}
Dheeraj
  • 59
  • 4
  • 1
    There is absolutely no difference. For function parameters, arrays decay to pointers. You don't get a copy of the array but only the address of first element. Also you cannot take the size of the parameter as it will only be size of a pointer. – Gerhardh Sep 08 '22 at 16:03
  • 1
    There is no difference, and any competent C engineer will see/know them as synonymous. The "which should be used", therefore, becomes at-best pure opinion, and essentially irrelevant. – WhozCraig Sep 08 '22 at 16:04
  • And, in _both_ functions, you can use either `*(ptr + 2)` or `ptr[2]` [preferred]. Also `++ptr` (which you can _not_ do in `main`) – Craig Estey Sep 08 '22 at 16:07
  • Not an exact dupe but the answers apply for you as well: https://stackoverflow.com/questions/1975128/why-isnt-the-size-of-an-array-parameter-the-same-as-within-main – Gerhardh Sep 08 '22 at 16:10
  • It should be noted (because “C/C++” exists in people’s minds) that it gets trickier in C++ — you can pass a _reference_ to an array in C++, which preserves array size information. See https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-a-p and https://stackoverflow.com/questions/2384107/how-does-this-template-magic-determine-array-parameter-size – Dúthomhas Sep 08 '22 at 16:17

2 Answers2

2

They're exactly the same.

If a parameter to a function is declared to have array type, that parameter is adjusted to pointer type.

This is spelled out in section 6.7.6.3p7 of the C standard regarding Function Declarators:

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.

This is in part due to the fact that in most contexts an array decays to a pointer to its first element, so it's not even possible to pass an array to a function (although you can pass a pointer to an array).

From a readability standpoint, you might want to use the array syntax when you expect to receive a pointer to the first element of the array, while you might want to use the pointer syntax when you expect to receive a pointer to a single object.

For example:

void foo1(int *p)
{
    *p += 2;
}

void foo2(int p[], int len)
{
    int i;
    for (i=0; i<len; i++) {
        printf("%d\n", p[i]);
    }
}

You can use either syntax in both cases, but the particular syntax makes it more apparent what the function is operating on.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

Array to pointer conversion

When an array type is used in a function parameter list, it is transformed to the corresponding pointer type:

int f(int a[2]) and int f(int* a) declare the same function.

Since the function's actual parameter type is pointer type, a function call with an array argument performs array-to-pointer conversion; ...

Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11