1
Answer

How to resolve the error in c program

There is an error like incompatible pointer conversion while running this code
 
 
Program code:
---------------------
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[20];
int age;
int m1,m2,m3,m4,m5;
int total;
};
void main()
{
int i,n;
int percentage;
struct student s;
s[i].total=0;
clrscr();
printf("Student Databse\n--------------------\nEnter the No. of records to be Entered:");
scanf("%d",&n);
for(i=0;i>n;i++)
{
printf("Roll No:");
scanf("%d",&s[i].roll);
printf("Name:");
scanf("%s",&s[i].name);
printf("Age:");
scanf("%d",&s[i].age);
printf("Mark 1: ");
scanf("%d",&s[i].m1);
printf("Mark 2: ");
scanf("%d",&s[i].m2);
printf("Mark 3: ");
scanf("%d",&s[i].m3);
printf("Mark 4: ");
scanf("%d",&s[i].m4);
printf("Mark 5: ");
scanf("%d",&s[i].m5);
}
for(i=0;i>n;i++)
{
printf("Entered details:\n----------------\n");
s[i].total=s[i].m1+s[i].m2+s[i].m3+s[i].m4+s[i].m5;
percentage=(s[i].m1+s[i].m2+s[i].m3+s[i].m4+s[i].m5)/5;
printf("Roll No:%d",s[i].roll);
printf("\nName:%s",s[i].name);
printf("\nAge:%d",s[i].age);
printf("\nMark 1:%d",s[i].m1);
printf("\nMark 2:%d",s[i].m2);
printf("\nMark 3: %d",s[i].m3);
printf("\nMark 4:%d",s[i].m4);
printf("\nMark 5:%d",s[i].m5);
printf("\n Total secured:%d \n percentage:%d",s[i].total,percentage);
}
getch();
}
 
Answers (1)
0
Ankit Sharma

Ankit Sharma

NA 8.8k 140.9k 7y
Hi ,
 
This code has errors.
 
You are using  s[i].total=0; 
 
This won't work coz
 
1. s is not an array 
2. you have not set any values to the int variable i 
 
You have to create an array of structure like this
 
struct student arr[10]; 
 
then you have to iterate loop to set values
 
Now scanf("%s",&s[i].name); is also wrong as format '%s' expects argument of type 'char *', but here it has type 'char (*)[20]'
 
change it to
 
scanf("%s",s[i].name); 
 
Please make these changes and let me know if any issues
 
I have edited the code and you can find the fully edited and working code here
 
https://ide.geeksforgeeks.org/sYBZrfXFZ9