c - C 中的参数过多和 char[22] 错误
问题描述
我在这段代码的末尾收到 2 个错误: 1 - 函数调用中的参数过多,表达式必须具有结构或联合类型,但它的类型分别为“char*”。我哪里做错了?
#include <stdio.h>
char soma_multipla_vetor(int vetor[], char operation) {
int i;
double total = 0;
double result;
char size;
for (i = 0; i < size; i++) {
total = total + vetor[i];
}
if (operation == 'm') {
result = total / size;
} else {
result = total;
}
return 0;
}
int main()
{
double result;
int i;
int vetor[5];
printf("Type 5 numbers\n");
for (int i = 0; i <= 5; i++) {
scanf("%d", &vetor[1]);
}
result = soma_multipla_vetor(vetor, 5, 'm');
^
printf("The result is %.2lf.\n". result);
} ^
解决方案
当你调用 时soma_multipla_vetor
,你给它 3 个参数:
result = soma_multipla_vetor(vetor, 5, 'm');
但它只期望2:
char soma_multipla_vetor(int vetor[], char operation) {
您还在size
此函数内部使用而不对其进行初始化。大概你希望 this 作为一个参数,所以将它添加到函数定义中以匹配它的调用方式:
char soma_multipla_vetor(int vetor[], int size, char operation) {
对于这一行:
printf("The result is %.2lf.\n". result);
您在字符串常量上使用.
仅适用于 a 的运算符。struct
您想要 a,
来分隔参数:
printf("The result is %.2lf.\n", result);
您在以下代码中也有问题main
:
for (int i = 0; i <= 5; i++) {
scanf("%d", &vetor[1]);
}
您总是使用索引 1 写入元素,而不是使用i
. 循环条件也不正确,因为它应该使用<
而不是<=
.
推荐阅读
- java - 是否可以访问具有私有构造函数的类中的私有变量并从另一个类更改最终变量值
- r - 更新闪亮的多个输入时的挑战
- react-native - jest.spyOn 未调用
- php - 头像wordpress中的安全图像调用不安全图像
- android - 在没有dependsOn的任务中运行评分任务
- javascript - 函数原型
- opencv - OpenCV vs 3rd 方人脸识别 SDK
- automated-tests - 由于从 UFT 中的 UI 自动化继承的 API 调用缺失或损坏而失败
- php - 服务器端缓存与客户端缓存以维护键值映射
- python - 在我自己的笔记本电脑上连接到在树莓派上运行的 MySQL