简体   繁体   English

int []和int *之间的区别作为函数参数

[英]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. int []int*之间有什么区别,它们都是函数的输入参数。

f(int a[] , int* b)

My answers: 我的答案:

For f() , they have the same functions. 对于f() ,它们具有相同的功能。 The first one is the beginning position of the first element in a[] . 第一个是a[]第一个元素的起始位置。

The second one points to an int . 第二个指向一个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. 作为函数参数,两种类型完全相同, int []被重写为int * ,您无法区分它们。 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). StackOverflow中的许多问题涵盖了这个主题,而c-faq甚至还有一个关于指针和数组的特殊部分 (作为参数与否)。 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. 如果不传递一些额外信息,您无法区分它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM