Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between int [] and int* as function arguments

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 ?

like image 602
user1002288 Avatar asked Dec 02 '22 01:12

user1002288


1 Answers

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.

like image 146
sidyll Avatar answered Dec 04 '22 09:12

sidyll