错误:请求成员'名称'不是结构或联合

时间:2022-01-14 21:21:04

I am trying to write a program that will read data from a text file(data2.txt) into a dynamic array. and then print that data out onto the screen. When I run my program I am getting the error shown below:

我正在尝试编写一个程序,将文本文件(data2.txt)中的数据读入动态数组。然后将该数据打印到屏幕上。当我运行我的程序时,我收到如下错误:

Error: request for member 'Name' in something not a structure or union.

I don't understand why I am getting this error since I have included a header file in my program that includes a structure. The size of the array P is the first integer number in the file. Any help\guidance will be greatly appreciated.

我不明白为什么我收到此错误,因为我在程序中包含了一个包含结构的头文件。数组P的大小是文件中的第一个整数。任何帮助\指导将不胜感激。

Here is the content of the text file data2.txt:

这是文本文件data2.txt的内容:


5
Martin Smith     22 2.2
Austin Clinton   18 3.1
Johnson          19 2.9
Maggie Jones     23 2.3
Tyler W Brown    16 3.4 

here is the content of the header file:

这是头文件的内容:


typedef struct RECORD
{
char Name[15];
int Age;
float Gpa;

};

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Mo.h"

int main()
{

int n;
int number = 0;
int i=0;


FILE * fpointer;
    fpointer = fopen("data2.txt", "r");

    fscanf(fpointer,"%d",&number);
    n = number;

    printf("Size value is: %d \n", n);

   int *P = malloc(n*sizeof(int));

[ERROR Showing on next line]

[ERROR显示在下一行]

  fscanf(fpointer,"%s %d %f", P[i].Name , &P[i].Age, &P[i].Gpa);

   while(! feof(fpointer))
   {
    printf("%s %d %.1f\n",P[i].Name , P[i].Age, P[i].Gpa);
     fscanf(fpointer, "%s %d %f",P[i].Name , &P[i].Age, &P[i].Gpa);

     i++;
   }



free(P);


fclose(fpointer);

printf("\n\n");
system("PAUSE");
return 0;
}

1 个解决方案

#1


2  

int *P = malloc(n*sizeof(int));

Here P is a pointer of type int what you need is

这里P是一个int类型的指针,你需要的是什么

struct RECORD *p = malloc(n * sizeof(struct RECORD));

p should be a pointer of type struct RECORD

p应该是struct RECORD类型的指针

#1


2  

int *P = malloc(n*sizeof(int));

Here P is a pointer of type int what you need is

这里P是一个int类型的指针,你需要的是什么

struct RECORD *p = malloc(n * sizeof(struct RECORD));

p should be a pointer of type struct RECORD

p应该是struct RECORD类型的指针