iO Ninja iO Ninja -4 years ago 120
C Question

Passing arrays in C: square brackets vs. pointer

I'm wanting to pass an array into a function. From what I can see, there are 2 ways of doing this:

1.

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


2.

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


Each one is called by:

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


Is there any actual difference between these 2 approaches?

Answer Source

There is no difference apart from the syntax. For historical reasons, even though int array[] looks like it should pass an array, it actually passes a pointer (meaning it's the same as int *array).

If I were you, I would prefer int *array just because it does what it looks like it does - that is, it's less likely to confuse you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download