This is an interview question:
What is difference between int []
and int*
, all of them are input arguments to a function.
f(int a[] , int* b)
My answers:
For f()
, they have the same functions. The first one is the beginning position of the first element in a[]
.
The second one points to an int
.
But, how to distinguish them from each other without passing other arguments ?
As function parameters, the two types are exactly the same, int []
is rewritten to int *
, and you can't distinguish between them. Many many questions in StackOverflow cover this subject, and the c-faq even has a special section on pointer and arrays (as arguments or not). Take a look into it.
In the context of function arguments, they are identical. All of the following are exactly the same:
f(int a[], int *b)
f(int a[], int b[])
f(int *a, int *b)
f(int *a, int b[])
You cannot distinguish between them without passing some extra information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.