2

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?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
ArunJTS
  • 264
  • 5
  • 15
  • This has been addressed in another Stack Overflow question. Checkout [Anonymous union within struct not in c99?](http://stackoverflow.com/questions/3228104/anonymous-union-within-struct-not-in-c99) – R Sahu Mar 01 '14 at 05:28
  • Check out: in each situation, is the GNU extension enabled? In another way, what standard did you compile each program? – Yu Hao Mar 01 '14 at 06:12
  • @Yu Hao, The bigger application, which gives the compilation error is compiled with '-std=c99 -ansi'. – ArunJTS Mar 01 '14 at 06:36
  • @Yu Hao, @ R Sahu, After disabling the '-std=c99 -ansi' from the Makefile, the code is getting compiled in GCC 4.1.1 itself. Thanks! – ArunJTS Mar 01 '14 at 06:41

2 Answers2

1

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.

ArunJTS
  • 264
  • 5
  • 15
0

Try giving your union a name.

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

Then access the vector like:

p_DB->outvec.someName.vector
831
  • 27
  • 2
  • True, but that's not the question. – Yu Hao Mar 01 '14 at 05:36
  • 1
    @YuHao The question was "Can somebody help?", and this is the help that's needed. – Jim Balter Mar 01 '14 at 05:48
  • @JimBalter In my opinion, the question is why this code compiles in GCC 4.6.1 but not GCC 4.1.1, like what's in the title. – Yu Hao Mar 01 '14 at 05:52
  • You're entitled to your opinion, but it's wrong -- the OP says it compiles in 4.1.1 but not 4.6.2. Anyway answer does address the OP's problem, but it could be a lot better. But we should encourage newcomers to answer questions. – Jim Balter Mar 01 '14 at 05:57
  • @JimBalter I read again, I misread the question, because it doesn't compile in the bigger application in 4.1.1 also. But I still the question is about why the code compiles sometimes but not in others. So this answer doesn't answer the question specifically. And how does this has anything to do with encouraging newcomers? I give a comment to show what I think about his answer, if he thinks it's helpful and improves his answer, I'm happy to upvote his answer. – Yu Hao Mar 01 '14 at 06:10
  • The issue is resolved without modifying the source code. It is fixed by changing the compilation setup. (Disabling the 'std=c89') – ArunJTS Mar 05 '14 at 05:34