在函数参数为int *阵列和int数组[]的区别 [英] Difference between int * array and int array[] in a function parameter
本文介绍了在函数参数为int *阵列和int数组[]的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到,如果函数参数是int ARR []或INT *改编数组值会发生变化。哪里不同?
int数组[]:
无效myFunction的(INT ARR [],INT大小){ 的for(int i = 0; I<大小; ++ I)
改编由[i] = 1;
}
为int *数组:
无效myFunction的为(int *改编,诠释大小){ 的for(int i = 0; I<大小; ++ I)
改编由[i] = 1;
}
这两个函数改变数组值。
INT的main(){ int数组[3]; 数组[0] = 0;
阵列[1] = 0;
阵列[2] = 0; myFunction的(排列3); 返回0; }
解决方案
有什么区别。这两个函数类型(调整后)的功能采取指针 INT
和 INT
,返回无效
。这是C ++只是一个语法怪癖:最外层的 []
非引用类型的函数参数与的代名词*
。
I have seen that the array values will change if the function parameter is "int arr[]" or "int * arr". Where is the difference?
int array[]:
void myFunction(int arr[], int size) {
for (int i = 0; i < size; ++i)
arr[i] = 1;
}
int * array:
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;
}
解决方案
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 *阵列和int数组[]的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文