首页 > 解决方案 > 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);
}          ^

标签: c

解决方案


当你调用 时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. 循环条件也不正确,因为它应该使用<而不是<=.


推荐阅读