C ++ * vs [] as a function parameter

What's the difference between:

void foo(item* list) { cout << list[xxx].string; } 

and

 void this(item list[]) { cout << list[xxx].string; } 

Suggested element:

 struct item { char* string; } 

With a pointer pointing to the first of an array of characters

and list is just an array of elements ...

+6
source share
3 answers

The compiler makes no difference.

He reads different though. [] suggests you pass an array to a function, while * can also mean a simple pointer.

Note that arrays decay into pointers when passed as parameters (in case you don't already know).

+8
source

They are the same - completely synonymous. The second is item list[] , not item[]list .

However, it is commonly used [] when the parameter is used as an array and * when it is used as a pointer.

+3
source

FYI:

 void foo(int (&a)[5]) // only arrays of 5 int are allowed { } int main() { int arr[5]; foo(arr); // OK int arr6[6]; foo(arr6); // compile error } 

but foo(int* arr) , foo(int arr[]) and foo(int arr[100]) equivalent

+1
source

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


All Articles