gpt4 book ai didi

c - 表达式必须有类类型错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:51 25 4
gpt4 key购买 nike

我正在做家庭作业,但碰壁了。我想我已经拥有了我需要的所有代码,我只需要编译程序即可。赋值的对象是

创建一个结构来保存学生姓名和平均分。该结构应包含名字、姓氏和整数平均成绩。

然后:

编写一个程序来执行以下操作:

1.) Create an array of pointers to these student structures.

2.) Prompt the user for names and averages.

3.) After you get the student’s information use malloc to provide the memory to store the information.

4.) Place the address of the student, returned by malloc, into the pointer array.

5.) AFTER the user indicates there are no more students:

搜索输入的数据并找到最高和最低等级 平均值。

a)打印最高等级的姓名和等级

b)打印最低年级的姓名和年级

c)打印所有输入成绩的平均值

这是我的代码:

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>


#define SIZE 25


int enterStudents (int ePointArray[SIZE]);
void searchData (int *sPointArray, int *sHigh, int *sLow);
int calculateAvg (int, int *avgPointArray);
void printData (int, int *pHigh, int *pLow);


struct student
{
char firstName[20];
char lastName[20];
int average;
};


int main()
{
int pointArray[SIZE], high[3], low[3];
int i = 0, studentCounter, avgGrade;


for (i = 0; i < SIZE; i++)
pointArray[i] = 0;


studentCounter = enterStudents(pointArray);


searchData(pointArray, high, low);


avgGrade = calculateAvg(studentCounter, pointArray);


printData(avgGrade, high, low);




return 0;
}




int enterStudents (int ePointArray[SIZE])
{
char tempFname[20], tempLname[20], yesNo[2] = "y";
int tempAvg, counter = 0;
int *studPtr;

struct student aStud={"\0", "\0", 0};




while( counter < SIZE && strcmp(yesNo, "y")==0)
{
printf(" Enter first name: ");
scanf("%s", tempFname);


printf(" Enter last name: ");
scanf("%s", tempLname);


printf(" Enter grade average:");
scanf("%d", tempAvg);


strcpy(aStud.firstName, tempFname);
strcpy(aStud.lastName, tempLname);
aStud.average = tempAvg;


studPtr = malloc(sizeof(struct student));


ePointArray[counter] = *studPtr;


counter++;


printf("/n");
printf(" Do you have more students? yes or no:");
scanf("%s", yesNo);
}


return counter;


}

void searchData (int sPointArray[SIZE], int sHigh[3], int sLow[3])
{
int searchCounter = 0;


while( searchCounter = 0)
{
if( *sPointArray[searchCounter].average > *sPointArray[searchCounter+1].average)
{
sHigh[0] = &sPointArray[searchCounter].firstName;
sHigh[1] = &sPointArray[searchCounter].lastName;
sHigh[2] = &sPointArray[searchCounter].average;
}

if( *sPointArray[searchCounter].average < *sPointArray[searchCounter+1].average)
{
sLow[0] = &sPointArray[searchCounter].firstName;
sLow[1] = &sPointArray[searchCounter].lastName;
sLow[3] = &sPointArray[searchCounter].average;
}


searchCounter++;
}


}


int calculateAvg( int totalStudents, int avgPointArray[SIZE])
{
int sum = 0;
int avgCounter;
double overallAvg;


for( avgCounter = 0; avgCounter < totalStudents; avgCounter++)
sum = sum + *avgPointArray[avgCounter].average;


overallAvg = sum/totalStudents;


return overallAvg;
}




void printData (int pAverage, int pHigh[3], int pLow[3])
{
printf(" Highest Grade: %s %s %d", pHigh[0], pHigh[1], pHigh[3]);
printf("/n");
printf(" Lowest Grade: %s %s %d", pLow[0], pLow[2], pLow[3]);
printf("/n");
printf(" Average Grade: %d",pAverage);


}

主要问题来自 searchData 函数。在 if 语句中,每次出现 *sPointArray 和 &sPointArray 时都带有红色下划线,错误显示为

 "Error: expression must have class type" 

同样的事情也发生在 for 循环中带有 *avgPointArray 的 calculateAvg 函数中。我知道这个错误对于新手 C 程序员(即我自己)来说是一个相当普遍的问题,并且它通常与将代码编写为函数而不是语句或类似的东西有关,但我不能终生我发现我哪里出错了。任何帮助将不胜感激。我在这方面工作了这么久,我的视线已经模糊了。

此外,如果有人在两秒钟内解决了这个问题并想证明我是个真正的白痴,那么 enterStudents 函数中有一个错误,它说 StudPtr = malloc(sizeof...)。错误显示在赋值符号下并显示

“错误:无法将类型为“void*”的值分配给类型为“int*”的实体。

我在理论上理解这个概念,但非常感谢有关如何修复它的一些建议。

提前感谢您的帮助。

最佳答案

您将 sPointArray 声明为整数数组,但将其用作结构数组。

关于c - 表达式必须有类类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15919782/

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