美文网首页
typedef 的一个错误

typedef 的一个错误

作者: 郭青耀 | 来源:发表于2017-03-14 07:04 被阅读0次

请看下面一个结构体定义,它会在什么时候报错?

typedef struct tagST
{
int a;
int b;
typdef union
{
struct BF *pstNew;
}new;
}XXX;
如果你能答道是在 “typdef union” 行报错,而且也知道是为什么,那么你就没有必要看下去了。否则请继续……

错误在于:

typedef struct tagST
{
int a;
int b;
typdef union
{
struct BF *pstNew;
}new; /这里只是声明/
}XXX;
于是声明中嵌套声明,是不是很奇怪,这里面需要的是一个定义,而不是声明。

正确的写法

typedef struct tagST
{
int a;
int b;
union
{
struct BF *pstNew;
}new; /这里是一个匿名的union 定义/
}XXX;

另外一种正确的写法

typdef union
{
struct BF *pstNew;
}newST;
typedef struct tagST
{
int a;
int b;
newST stNew;
}XXX;
声明了一个 union ,然后在结构体里面使用。

相关文章

网友评论

      本文标题:typedef 的一个错误

      本文链接:https://www.haomeiwen.com/subject/etdcnttx.html