C 中的两个函数有什么区别吗?
void f1(int arr[]) {
//some code...
}
void f2(int arr[10]) {
//some code
}
f1 函数中第一个数组的大小是多少?
最佳答案
Is there any difference between two function in c?
这里没有区别。两者都将被编译器解释为 int *arr
,因为数组在用作函数参数时会转换为指向其第一个元素的指针。
what will be the size of first array in f1 function?
严格来说,这里没有数组。它是指向 int
的唯一指针。如果您将使用 sizeof(arr)
,那么您将获得等于 sizeof(int *)
的值。
当参数类型是指向数组的指针时,需要参数中的数组大小。在这种情况下,您需要指定数组的大小,因为每个大小都会使指针指向不同的类型。
void f3(int (*arr)[10]); // Expects a pointer to an array of 10 int
关于c - foo(int arr[]) 和 foo(int arr[10]) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043339/