2015-05-23 42 views
-1

我不知道我的代码在这里有什么问题。我读了一些有同样问题的人的问题,但我没有找到答案。 当我尝试编译我得到这个错误:错误:'DATA/*之前的期望表达式:typedef struct DATA DATA */

||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; 
    } 
+0

'sizeof DATA'应该是'sizeof(DATA)'在'sizeof'中键入名字所需的括号 – BLUEPIXY

+0

我明白了,谢谢; sizeof unary_xpression;或sizeof(类型)是语法 –

回答

1

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

代替

DATA *HEAD = MALLOC(sizeof DATA); 

正如你所看到的类型名称必须用括号括起来sizeof操作符是指通过以下方式

sizeof unary-expression 
sizeof (type-name) 

从C标准(6.5.3.4 sizeof运算和alignof运营商)

2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.

考虑到,如果e是一个表达式然后(e)也是一个表达式。因此,对于表达式可能写任

sizeof e 

sizeof(e) 

和可变的标识符也形成一个表达式。

1

变化

DATA *HEAD = MALLOC(sizeof DATA); 

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

类型名的操作数sizeof运营商必须要加上括号。使用

+0

这是我的问题,我读到,当它不是一个基本类型(int,char ..)时,我不应该括住它,现在我的问题是:什么时候应该用括号括起来,何时不用? –

+0

'sizeof'接受表达式或类型名称操作数。对于表达式来说,括号是可选的(例如'sizeof 42'或'sizeof(42)'),对于类型名称,它们是必需的(e..g,'sizeof(int)')。 – ouah