The difference between int [] and int * as function arguments

This is an interview question:

What is the difference between int [] and int* , they are all input arguments to the function.

 f(int a[] , int* b) 

My answers:

For f() they have the same functions. The first is the starting position of the first element in a[] .

The second points to int .

But how to distinguish them from each other without passing other arguments?

+4
source share
2 answers

As functional parameters, the two types are exactly the same, int [] written to int * , and you cannot distinguish between them. Many questions in StackOverflow cover this topic, and c-faq even has a special section on pointers and arrays (as arguments or not). Take a look at it.

+12
source

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 transmitting additional information.

+6
source

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


All Articles