【问题标题】:what "[Error] invalid operands to binary + (have 'float' and 'float *')" means“[Error] 二进制 + 的无效操作数(具有 'float' 和 'float *')” 是什么意思
【发布时间】:2021-06-02 13:47:10
【问题描述】:

ATYB 银行在未来三年为 5 名学生提供大学奖学金
使用指针符号输入每个学生的各种奖学金金额 使用一个名为 totalschol 的模块,它有 2 个参数、一个数组和一个指向该数组的指针。 该模块输出(使用指针表示法)每个学生未来三年的总金额

#include <stdio.h>
#include <stdlib.h>

// Function Prototype..
void totSchol(float total [], float *aPointer);

int main(void)
{
    // Declare Local Variables..
    int i;
    float amt[5]={0}, tot;
    //points to the array variable.
    float *aPointer = &amt[0];
    //prompts AYTB Bank..
    printf("\t\t AYTB Bank\n");
    for (i=0; i<5; i++)
    {
        //prompts to enter scholarship amount for 5 students..
        printf("Student %d, Enter scholarship amount: ", i+1);
        //takes value entered and store in pointer variable aPointer & increment by i..
        scanf("%f", aPointer + i);
        tot += aPointer;
    }

    totSchol(amt, aPointer);
    
    return 0;
}


void totSchol(float total [], float *aPointer)
{
    int x;
    float year3;
    
    for (x=0; x < 5; x++)
    {
        
        printf("\nThe total amount for each studetn is: %.2f", (aPointer + x));
        year = (aPointer +x) * 3;
        printf("\nThe total amount for each studetn after 3 years is: %.2f", year3);
        
    }
}

【问题讨论】:

  • 哪一行导致错误?
  • tot += aPointer;
  • totScholaPointer + x的使用也不正确,你的意思是aPointer[x]

标签: c function function-pointers


【解决方案1】:

错误的意思正是它所说的 - 您试图将 float 添加到 float * 并且这是不允许的(您只能将整数添加到指针,结果是另一个指针)。它发生在这里:

tot += aPointer;

你可能打算写

tot += aPointer[i];

aPointer 指向amt 数组的第一个元素,所以aPointer[i] 等价于amt[i]

【讨论】:

  • 非常感谢,像这样简单的事情可能会弄乱我看到的代码,而且我在我的函数定义中添加了year3= aPointer[x] * 3;。它的工作原理谢谢...
猜你喜欢
  • 2017-07-06
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多