arrays - getting error on passing array to function in C
问题描述
I have tried to use function to calculate the sum of elements in array. Logic is correct but I think there is some syntax error but I am not getting it. Please help me in solving this.
#include <stdio.h>
int add(int arr[]);
int main(void)
{
int nums[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 };
int result = add(nums);
printf("Result: %d\n", result);
return 0;
}
int add(int arr[])
{
int total = 0;
int i;
for (i = 0; i < sizeof(arr)/sizeof(arr[0]); i++) {
total += arr[i];
}
return total;
}
The error I am facing is this ->
main.c:16:27: warning: ‘sizeof’ on array function parameter ‘arr’ will return size of ‘int *’ [-Wsizeof-array-argument] for (i = 0; i < sizeof(arr)/sizeof(arr[0]); i++) { ^ main.c:12:13: note: declared here int add(int arr[]) ^~~ Result: 3
解决方案
推荐阅读
- python - Selenium 动作链复制鼠标动作
- shell - 编写 git commit 脚本时出现 vagrant up 错误
- javascript - JavaScript:- 将月份添加到对象数组
- ruby - 自定义 Jekyll 插件(液体过滤器?)将脚本标签呈现为文本,而不是 JavaScript
- java - 我怎样才能让我的循环不重新打印它的旧值?
- android - 在颤动的背景中从android中获取位置
- javascript - 在 WebStorm 中使用 require() 后 - 未解析的函数或方法
- c# - Asp.Net MVC 根据另一个表中的设置显示索引列表
- r - 类似于 R 中的 Pandas Series.value_counts()?
- r - 提取字符串中的所有单词和字母簇,然后使用 R 中的 gsub() 使每个单词成为单独的数据片段