函数参数中 int * array 和 int array[] 的区别 [英] Difference between int * array and int array[] in a function parameter
本文介绍了函数参数中 int * array 和 int array[] 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到如果函数参数是int arr[]"或int * arr",数组值会发生变化.区别在哪里?
I have seen that the array values will change if the function parameter is "int arr[]" or "int * arr". Where is the difference?
整数数组[]:
void myFunction(int arr[], int size) {
for (int i = 0; i < size; ++i)
arr[i] = 1;
}
int * 数组:
void myFunction(int * arr, int size) {
for (int i = 0; i < size; ++i)
arr[i] = 1;
}
两个函数都会改变数组值.
Both functions change the array values.
int main(){
int array[3];
array[0] = 0;
array[1] = 0;
array[2] = 0;
myFunction(array, 3);
return 0;
}
推荐答案
没有区别.两种函数类型(调整后)都是函数采用指向 int
和 int
的指针,返回 void
".这只是 C++ 的一个语法怪癖:非引用类型的函数参数中最外层的 []
与 *
同义.
There is no difference. Both functions types (after adjustment) are "function taking a pointer to int
and an int
, returning void
." This is just a syntactic quirk of C++: the outermost []
in a function parameter of non-reference type is synonymous with *
.
这篇关于函数参数中 int * array 和 int array[] 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文