c - foo(int arr[]) 和 foo(int arr[10]) 有什么区别?

标签 c function

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/

相关文章:

javascript - 重复回调函数的最佳实践?

c - stdout 上的 freopen_s 会导致 Windows 上的 GetConsoleScreenBufferInfo 出现问题

c - C中的四维整数数组

c - 对函数调用的 undefined reference ?

javascript - 如果一个函数在另一个函数内部执行,它会立即符合回调函数的条件吗?

javascript - slider 功能延迟

c++ - 发送可变大小的二维数组以在 C++ 中运行

php - 如何计算页面加载时有多少查询?

c++ - BFS 可以找到图中的环吗?

c - while 循环中带有 scanf 的无限循环