Bhavna
Bhavna

Reputation: 115

Array of Structures in C. not linked list

I am having trouble making an array of structures. I want to store data regarding multiple students and take input using the execution of the program. Then output the memory address of persons name with highest marks.

I tried getting to know the answers on similar issues on site, to no help.

The code I have written is

the struct

struct student{
    char name;
    int marks;
};

and the function is

int num,max = 0,i;
    printf("Number of students?\n");
    scanf("%d", &num);
    struct student* students = (struct student*)malloc (num* (sizeof(struct student)));
    for (int x = 0; x < num; x++)
    {
        printf("Enter Name\n");
        scanf("%c" , &students[x].name);
        printf("Enter Marks\n");
        scanf ("%d" , &students[x].marks);
    }   
    for (i = 1; i < num; i++)
    {
        if (students[max].marks<students[i].marks)
        {
            max = i;
        }
    }
    printf("The Memory Add for %c is %p\n", students[max].name , &students[max].name );

It is a part of menu driven program. It goes into a weird loop after line 5 in function code. And i can't create a doubly linked list for the same.

Edit 1

These are the changes i made to struct and func

struct student{
    char name[20];
    int marks;
};


void memadd(){
    int num,max = 0,i, mark;

    printf("Number of students?\n");
    scanf("%d", &num);
    struct student* students = (struct student*)malloc (num* (sizeof(struct student)));
    for (int x = 0; x < num; x++)
    {
        printf("Enter Name\n");
        scanf("%s" , &students[x].name);

        printf("Enter Marks\n");
        scanf ("%d" , &mark);
        students[x].marks = mark;
    }   
    for (i = 1; i < num; i++)
    {
        if (students[max].marks<students[i].marks)
        {
            max = i;
        }
    }
    printf("The Memory Add for %s is %p\n", students[max].name , &students[max].name );

It is giving an array at input of the name. "format specifies type 'char ' but the argument has type 'char ()[20]'"

How do I make the format to be char * [20]?

Edit 2 The final working code after correcting the char name to being a character array And issue with scanning of names.

struct student{
    char name[20];
    int marks;
};
int num,max = 0,i, mark;

    printf("Number of students?\n");
    scanf("%d", &num);
    struct student* students = (struct student*)malloc (num* (sizeof(struct student)));
    for (int x = 0; x < num; x++)
    {
        printf("Enter Name\n");
        scanf("%19s" , students[x].name);

        printf("Enter Marks\n");
        scanf ("%d" , &mark);
        students[x].marks = mark;
    }   
    for (i = 1; i < num; i++)
    {
        if (students[max].marks<students[i].marks)
        {
            max = i;
        }
    }
    printf("The Memory Add for %s is %p\n", students[max].name , &students[max].name );

However, what if I wish to print the address in Hex numbers? or the one that am getting is hex already.? Because I got an output of The Memory Add for XYZ is 0x7fb340c03928

Upvotes: 2

Views: 176

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74058

The problem is with the student name being a char and

scanf("%c" , &students[x].name);

What happens is, you read in the number of students with

scanf("%d", &num);

Now you have read num, but there is still a newline in the input buffer. Then you try to read a character with %c, but this reads the remaining newline.

When you change the student's name to a character array, e.g.

struct student{
    char name[20];
    int marks;
};

and read the name with

scanf("%19s" , students[x].name);

it will skip the remaining whitespace/newline and read the name as it should.

Upvotes: 2

Related Questions