4

Possible Duplicate:
Difference between passing array and array pointer into function in C

I have been wondering this for a while, is there any difference between these two?

void f1(char *c);
void f2(char c[]);

A common example is this:

int main(int argc, char **argv);
int main(int argc, char *argv[]);

Are there any reasons to prefer one to the other, apart from artistic ones?

Community
  • 1
  • 1
orlp
  • 112,504
  • 36
  • 218
  • 315
  • 1
    http://stackoverflow.com/questions/5573310/difference-between-passing-array-and-array-pointer-into-function-in-c – chepner Apr 20 '12 at 14:20

4 Answers4

6

There is no difference.

From the horse mouth:

(C99, 6.7.5.3p7) "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."

On the reasons to prefer one form over the other, it depends on the people. Here is what H&S says (on switching to T *array from T arr[]):

(H&S, 5.4.3 Array Bounds) "That would more accurately reflect the implementation but less clearly indicate the intent."

ouah
  • 142,963
  • 15
  • 272
  • 331
2

No. In a function declaration/definition, the use of an array as an argument is syntactic sugar only. It's still passed as a pointer, and sizeof(theArgument) will still give you sizeof(TheType *) rather than sizeof(TheType) * sizeof(numElements).

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
1

Well first one indicates that this is a pointer and the second that this is an array. While there is no effective difference, when you read the code you generally expect a single element in the first case an a sequence of more then one element in the second case. This might be helpful sometimes.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

There is no difference. Every n-dimensional array in both C and C++ can be interpreted as both n-dimensional array and pointer to (n-1)-dimensional array (as the pointer's [] semantic is same as array's one).

Griwes
  • 8,805
  • 2
  • 43
  • 70