gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:55:58 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com