문제

In an C application, the following code is present.

#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;
}

The above code is getting compiled and executed, as an independent application.

But, when the structure DBType is used as part of a bigger application, the following line gives the error,

if (p_DB->outvec.vector == NULL) {

error: ‘PpetKeviParams’ has no member named ‘vector’**

The gcc version in the Linux machine is 4.1.1

The same code (bigger application) is getting compiled in gcc 4.6.2 machine.

I couldn't find the issue. Can somebody help?

도움이 되었습니까?

해결책

The above issue was because of the issue, 'unnamed union' in source code, in GCC 4.1.1 with 'std=c89 / c99'. After disabling the 'std=c89', the code is getting compiled in GCC 4.1.1, itself.

다른 팁

Try giving your union a name.

union {
    float *matrix;
    float *vector;
} someName;

Then access the vector like:

p_DB->outvec.someName.vector
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top