Passing arrays to C: square brackets against pointer

I want to pass an array to a function. From what I see, there are two ways to do this:

1.

void f (int array[]) {
    // Taking an array with square brackets
}

2.

void f (int *array) {
    // Taking a pointer
}

Each of them is called:

int array[] = {0, 1, 2, 3, 4, 5};
f (array);

Is there a real difference between the two?

+4
source share
3 answers

There is no difference except the syntax. For historical reasons, although int array[]it looks like it should pass an array, it actually passes a pointer (which means it is equal int *array).

If I were you, I would prefer int *arrayonly because he does what he looks like he is, that is, he is less likely to confuse you.

+4

.

, [] "" . , [], .

struct S;
void foo(struct S *a); // OK
void bar(struct S a[]); // ERROR

, canon void * void [].

, ( ).

+3

. , , . , , .

. C FAQ list.

" C" , " ", C, -.

+1

Source: https://habr.com/ru/post/1679003/


All Articles