请您解释此错误背后的原因。错误:使用struct type value((*; ptr; ptr ++)需要标量)
错误代码:
used struct type value where scalar is required
for(;*ptr;ptr++)
"for the below code?
是否有任何理由,我们是不允许结构变量在for循环?
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20]; // Given
int math; // Marks in math (Given)
int phy; // Marks in Physics (Given)
int che; // Marks in Chemistry (Given)
int total; // Total marks (To be filled)
int rank; // Rank of student (To be filled)
};
static int count=1;
void enter_data(struct student* arr,int num);
void print_data(struct student* arr,int num);
int main()
{
int num=1;
char ch;
printf("Enter the number of student records you want to input?..\n");
scanf("%d",&num);
struct student *arr=(struct student*)malloc(num*sizeof(struct student));
printf("Do you want to enter the student record...(Y/N)\n");
scanf(" %c",&ch);
if(ch=='y'||ch=='Y')
{
enter_data(arr,num);
printf("The created record is .....\n");
print_data(arr,num);
}
else
return 0;
}
void enter_data(struct student* arr,int num)
{
int i;
struct student* ptr=arr;
for(;count<=num;ptr++,count++)
{
printf("Enter the name of the candidate...\n");
scanf("%s",ptr->name);
printf("Enter the marks scored in maths,phy,che....\n");
scanf("%d%d%d",&ptr->math,&ptr->phy,&ptr->che);
((ptr->total)=(ptr->math)+(ptr->phy)+(ptr->che));
}
}
void print_data(struct student* arr,int num)
{
int i;
struct student* ptr=arr;
for(;*ptr!=NULL;ptr++)//error here
{
printf("Name of the candidate...%s\n",ptr->name);
printf("Enter the marks scored in maths...%d\t physics... %d\tche... %d\n total=%d\n",ptr->math,ptr->phy,ptr->che,ptr->total);
}
}
的误差。即你正在比较'struct student'到'NULL'。那个解除引用不应该在那里。我认为所报告的错误消息“对二进制表达式无效的操作数('struct student'和'void *')”会使这一点变得明显。而且,即使在修正之后,我不认为这是你想要的结果。你需要一个在这个循环中的计数器。 – WhozCraig
实际上,您需要使用'num'而不是'ptr'来终止该循环。 – aschepler
我同意@aschepler。该循环可能更像'for(; num--; ++ ptr)'或类似的。 'ptr'的值确实不属于条件。 – WhozCraig