<(>令牌之前的预期标识符)

时间:2013-05-16 19:45:34

标签: c

这段代码出了什么问题?

#include "stdio.h"

typedef int type1[10];

typedef type1 *type2;

typedef struct {
    int field1;
   type2 field2;
} type3;

typedef type3 type4[5];

int main() {    
   type4 a;
   a[0].(*field2[3]) = 99;  // Line 16
   return 0;
}

获取: main.c:16:10:错误:<(>令牌

之前的预期标识符

Gcc版本:gcc(GCC)4.7.2

3 个答案:

答案 0 :(得分:6)

编译错误告诉你完全错误:

  <(>令牌

)之前的

预期标识符

您只能使用其名称(标识符)访问结构成员,而不能通过某种任意表达式访问。

答案 1 :(得分:1)

检查errata for the book.

  

页。 18,l。 -7正确的表达式是(* a [0] .field2)[3] = 99;

     

Andrew Li 12/31/10

我不确定20年前首次出版的图书是否有一个好的或坏的迹象,这个图片在去年才发现了十几个错误(有些重要!)。

这本书可能很旧,但Jeff Ullman受到高度尊重。我记得几十年前他的编纂设计书籍让我的大脑变得...... ...

答案 2 :(得分:0)

如果这真的来自一本书,那么这是一个错字。分配int的一些正确表达式是

*a[0].field2[3] = 99;
*(a[0].field2)[3] = 99;
*(a[0].field2[3]) = 99;

工作计划将是

#include <stdlib.h>

typedef int type1[10];

typedef type1 *type2;

typedef struct {
   int field1;
   type2 field2;
} type3;

typedef type3 type4[5];

int main(void) {
   type4 a;

   a[0].field1 = 99;
   a[0].field2 = malloc (sizeof(type1));
   (*a[0].field2)[3] = 99;

   return 0;
}

请注意<stdlib.h>的使用,因为我们使用malloc