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

标签 c struct compiler-errors linked-list typedef

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

||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/

相关文章:

c - 从 rsp 反汇编 sub 和不同的函数返回

c - sizeof(&array)返回什么?

java - 在终端中编译 java 应用程序

c# - 在另一个方法内部有两种方法遇到麻烦

java - 不能从静态上下文中引用非静态变量

c - 无法在 gcc 的算术表达式中将 size_t 类型转换为 unsigned Short

C程序求第n个质数——

arrays - 过滤包含字符串和字符串数组的结构对象

Swift:闭包如何捕获值类型的变量?

ruby - 是否分配了 ruby​​ 结构堆栈?