【发布时间】:2014-04-02 08:37:26
【问题描述】:
在 C 应用程序中,存在以下代码。
#include <stdlib.h>
#include <string.h>
typedef struct
{
/*! matrix ID */
int id;
/*! number of rows */
int num_rows;
/*! number of columns */
int num_cols;
union {
float *matrix;
float *vector;
};
} PpetKeviParams;
typedef struct {
char DB_char;
int DB_index;
float DB_val;
PpetKeviParams outvec;
} DBType;
int main(void)
{
DBType *p_DB=(DBType *)malloc( sizeof(DBType));
if (p_DB->outvec.vector == NULL) {
printf("\t\t\tp_DB->outvec.vector is NULL\n");
}
if(p_DB != NULL) {
free(p_DB);
}
return 0;
}
上面的代码正在编译和执行,作为一个独立的应用程序。
但是,当结构 DBType 用作更大应用程序的一部分时,以下行给出错误,
if (p_DB->outvec.vector == NULL) {
错误:“PpetKeviParams”没有名为“vector”的成员**
Linux机器中的gcc版本是4.1.1
相同的代码(更大的应用程序)正在 gcc 4.6.2 机器中编译。
我找不到问题。有人可以帮忙吗?
【问题讨论】:
-
这已在另一个 Stack Overflow 问题中得到解决。结帐Anonymous union within struct not in c99?
-
检查:在每种情况下,是否启用了 GNU 扩展?换句话说,你编译每个程序的标准是什么?
-
@Yu Hao,较大的应用程序,编译错误是用'-std=c99 -ansi'编译的。
-
@Yu Hao,@R Sahu,从 Makefile 中禁用“-std=c99 -ansi”后,代码将在 GCC 4.1.1 中编译。谢谢!