指向结构来存储数据

时间:2014-10-14 04:46:48

标签: c arrays pointers structure fopen

程序应该从包含8位数字(id)和5个其他数字(标记)的文本文件中读取,用单个空格分隔。但我一直收到错误"要求会员'学生' in' * p',这是非类型的`char'"当我尝试指向数组来存储数据时?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 5

struct Students {
    char id[10];
    int mark[SIZE];
};

void readdata(Students *p);

int main() {

    int i, j, highesttotal, lowesttotal, highestmark, total, *ptr;
    char htid[10], ltid[10], hmid[10], *p;

    struct Students *q;
    q = (Students *)malloc(SIZE*sizeof(Students));

    FILE *f;

    f = fopen("student.txt", "r");
    if(f = NULL) { perror("Error"); exit(20); }

    for(i=0; i<SIZE; i++) {
        fscanf(f,"%s %d", p-> Students.id[i], ptr -> Students.mark[i]);
        i++;
    }

    total = highesttotal = highestmark = 0;
    lowesttotal = 100;

    for(i=0; i<SIZE; i++) {
        for(j=0; j<SIZE; j++) {
            total += Students.mark[j];
            if(p -> Students.mark[j] > (p -> highestmark)) {
                highestmark = Students.mark[i];
                hmid = (p -> Students.id[j]);
            }
            if(highesttotal < total) {
                highesttotal = total;
                htid = (p -> Students.id[j]);
                total = 0;
            }
            if(lowesttotal > total) {
                lowesttotal = total;
                ltid = (p -> Students.id[j]);
                total = 0;
            }
        }
    }
    printf("%s %d", ltid, lowesttotal);
    printf("%s %d", htid, highesttotal);
    printf("%s %d", hmid, highestmark);
}

1 个答案:

答案 0 :(得分:1)

该行

  

char htid[10], ltid[10], hmid[10], *p;

声明p是指向char的指针。因此,*pchar,它没有任何成员数据。您的意思是使用q吗?