Reputation: 11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int id;
char name[50];
double total;
char grade;
}student;
void Highest(student record[]);
int main()
{
int i;
student record[] = {{1234, "Safiya", 99.5, 'U'},
{1235, "Tahir", 94.6,'U'},
{1246, "Ay", 83.2,'U'},
{1564, "Ju", 40.2, 'U'},
};
for(i =0; i<4; i++)
{
if (record[i].total>80){
record[i].grade = 'A';
}
else
if(record[i].total>=50 && record[i].total<= 80){
record[i].grade = 'B';
}
else
if(record[i].total<50){
record[i].grade = 'C';
}
printf("\nRecord of Student %d:\n", i+1);
printf("ID: %d\t\tName: %s\t\tTotal: %.2f\t\tGrade: %c\n",
record[i].id, record[i].name, record[i].total, record[i].grade);
}
Highest(record);
return 0;
}
void Highest(student record[]){
double highest;
char highName;
highest = record[0].total;
for(int i=0; i<4; i++){
if(record[i].total>record[0].total)
{
highest = record[i].total;
highName = record[i].name;
}
}
printf("\n\nThe Highest graded student is %s and their grade is %.2f\n",
highName, highest);
}
When I tried compiling this code, I get -
"The Highest graded student is (null) and their grade is 99.50", the %s in the function to be as (null)
and the error:
"warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’".
How can I change that?
I tried to do as they suggested by changing char
to char *
, I get a number in the console.
Upvotes: 1
Views: 454
Reputation: 9814
You cannot use a char
to save a string or a pointer to it. You need a pointer for that.
Change char highName;
to char *highName = record[0].name;
. Now it will store a pointer to the name
field of the struct. You also have to initialize it, like in the example.
complete code: https://godbolt.org/z/5Yov69E6n
Upvotes: 1