Jiaming Huang
Jiaming Huang

Reputation: 101

How to Use Pointers to Structure to output?

There are 3 students in a class,now I invoke StuInfo(struct Student *p) to get the information of these 3 students including IDs, names and their scores. I invoke Rank(struct Student *p) to rank their scores and output the best one's information. But,everytime, the output of q->scores is 0.00000 and I don't know why.

here is the code:

#include<stdio.h>
struct Student 
{   
    int number;
    char name[20];
    double scores;
};
void StuInfo(struct Student *p);
void Rank(struct Student *p);
int main()
{   
    struct Student stus[3];
    struct Student *p;
    p=stus;
    StuInfo(p);
    Rank(p);
    return 0;
}
void StuInfo(struct Student *p) 
{
    printf("please enter the student'ID,name,scores:"); 
    for(int i=0;i<3;i++) 
    {
        scanf("%d%s%f",&(p+i)->number,&(p+i)->name,&(p+i)->scores);     
    }       
}

void Rank(struct Student *p)
{
struct Student *q;
    q=p; 
    for(int i=0;i<2;i++)
    {
        if(q->scores<(p+i+1)->scores)
        {           
            q=p+i+1;
        }   
    }   
    printf("the best student's ID%d,name%s,score%f",q->number,q->name,q->scores);
}

Upvotes: 1

Views: 75

Answers (2)

Brandon83
Brandon83

Reputation: 206

Here are a couple of tips I can suggest to make the code more readable and simple when dealing with pointer arithmetic.

  1. You will need to fix your scanf format specifier this is why you keep reading 0.0000 for the score. To fix it change the last %f to %lf.

  2. You can simplify all this pointer and offset that you have here:

    scanf("%d%s%f",&(p+i)->number,&(p+i)->name,&(p+i)->scores);

    to just the following:

    scanf("%d%s%lf", &p->number, &p->name, &p->scores);
    p++;

    Sincep is pointing to the size of a struct Student it will automatically increment to the next element when you do p++. This is much easier to read and allows for less of a chance to have pointer arithmetic errors!!

  3. You can clean up the last loop as well to avoid arithmetic errors and to simplify things a bit as follows:

    void Rank(struct Student *p)
    {
      struct Student *q;
      q = p;
      for (int i = 0; i<2; i++)
      {
        p++;
        if (q->scores < p->scores)
        {
          q = p;
        }
      }
      printf("the best student's ID: %d,name: %s,score: %f\n", q->number, q->name, q->scores)
    }
    

Hope this helps!

Upvotes: 0

Seek Addo
Seek Addo

Reputation: 1903

You have a little mistake in the scanf format, you need %lf for double not %f and the char array name don't need the address operator &(p+i)->name when using the pointer. If you want to use the & address operator for the name then you have to point to the first element of the array like this &(p+i)->name[0]

I part from that your code should work

void StuInfo(struct Student *p)
{
    printf("please enter the student'ID,name,scores:");
    for(int i=0;i<3;i++)
    {
        scanf("%d%s%lf",&(p+i)->number,(p+i)->name,&(p+i)->scores);// here
    }
}

Upvotes: 2

Related Questions