gpt4 book ai didi

c - 我什么时候在 scanf() 中使用 "&"?

转载 作者:行者123 更新时间:2023-11-30 20:05:20 25 4
gpt4 key购买 nike

这是我的代码:

int main() {
int i,n, *arr;
scanf("%d", &n);
arr = (int *)malloc(sizeof(int)*n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("%lld", sumOfArray(arr,n));

return 0;
}

为什么我必须使用&arr[i]?我认为当 arr 时不需要它类型为int *

最佳答案

scanf() 获取一个内存地址,并将其解析为将数据推送到的指针; arr 本身就是一个指针,但 arr[i] 是一个 int。因此,需要将arr[i]的地址传递给scanf()

关于c - 我什么时候在 scanf() 中使用 "&"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32319756/

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