gpt4 book ai didi

c - 错误: Expected expression before 'DATA/* : typedef struct DATA DATA */

转载 作者:行者123 更新时间:2023-11-30 18:39:18 27 4
gpt4 key购买 nike

我不知道我的代码有什么问题。我读了其他有同样问题的人的一些问题,但没有找到答案。当我尝试编译时出现以下错误:

||In function 'main':|

|35|error: expected expression before 'DATA'|

||In function 'lecture_data':|

|59|error: expected expression before 'DATA'|

||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

我还想知道如果有其他问题我应该做什么,以及在这段代码中我不应该做什么。

代码:

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

#define V 20

/* Structure */

typedef struct DATA{
char* NomP;
char* NomA;
int iNbr;
int iPix;
struct DATA *Next;
}DATA;

/* --------- */

void* MALLOC(size_t nombre);
int lecture_data(FILE *fs,DATA* Data,DATA* ROOT);
void print_data(DATA* data,int len);

int main(void)
{
char FileName[V];
puts("Data file ? : ");
gets(FileName);

FILE* fs = fopen(FileName,"r");
if( fs == NULL )
{
perror("Error ");
exit(EXIT_FAILURE);
}

DATA *HEAD = MALLOC(sizeof DATA);
DATA *D_Store;
int len = lecture_data(fs,D_Store,HEAD);
print_data(D_Store,len);
return 0;
}

int lecture_data(FILE *fs,DATA *Data,DATA *ROOT)
{
char cNom[V],cArticle[V];
int iNombre,iPrix;
int eofs=0;int i=0;

while(!eofs)
{
fscanf(fs,"%s %s %d %d",cNom,cArticle,&iNombre,&iPrix);
Data->iNbr=iNombre;
Data->iPix=iPrix;
Data->NomA = MALLOC(strlen(cArticle)+1);
Data->NomP = MALLOC(strlen(cNom)+1);
strcpy(Data->NomA,cArticle);
strcpy(Data->NomP,cNom);
if( i==0 )
{
Data -> Next = MALLOC(sizeof DATA);
ROOT = Data ;
}
DATA *Ptr = ROOT ;
while( Ptr -> Next != NULL )
{
Ptr = ROOT -> Next;
}
Data -> Next = NULL ;
Ptr -> Next = Data ;

fprintf(stdout,"Data : N.%d: %s %s %d$\n",i,cNom,cArticle,iNombre*iPrix);
i++;
eofs = feof(fs) ;

if(ferror(fs))
{
perror("Error ");
exit(EXIT_FAILURE);
}
}
fclose(fs);
return i;
}

void* MALLOC(size_t nombre)
{
void *MEMEORY = malloc(nombre);
if(NULL == MEMEORY )
{
perror("Error ");
exit(EXIT_FAILURE);
}
return MEMEORY;
}

最佳答案

改变

DATA *HEAD = MALLOC(sizeof DATA);

DATA *HEAD = MALLOC(sizeof (DATA));

sizeof 运算符的类型名称操作数必须加括号。

关于c - 错误: Expected expression before 'DATA/* : typedef struct DATA DATA */,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411513/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com