Topic : C-Ride and typedef union

Forum : 8051

Original Post
Post Information Post
March 29, 2007 - 6:59pm
Guest

Hello everybody,
I am new in the frorum and I am not a programer but now I am making my phinal thesis and I need program a 8051 micro.

My problem is that I have a: typedef union and after I want to use this typedef union insido of another typedef union.
Well to sum up, my code is:

typedef struct _SHORT_ADDR
{
struct _SHORT_ADDR_bits
{
BYTE MSB;
BYTE LSB;
} byte;
WORD Val;
BYTE v[2];
} SHORT_ADDR;

typedef union _ADDR
{
LONG_ADDR LongAddr;
SHORT_ADDR ShortAddr;

BYTE v[8];
} ADDR;

Well, I want to use SHOR_ADDR inside of ADDR typedef and the compiler gives the C144 ERROR.

How could I solve this?
Is forbiden use union inside of unions? and struct inside of struct???

Thanks!
Santi

Replies
Post Information Post
+1
0
-1
March 29, 2007 - 7:25pm
Guest
[pre]should that not betypedef union _ADDR
{
SHORTADDR LongAddr;
SHORT_ADDR ShortAddr;

BYTE v[8];
} ADDR[/pre]

you have no definition of LONG_ADDR

+1
0
-1
March 30, 2007 - 8:56am
Guest

I made a mistake in the first post but in my program it is rigth, well the program is:

typedef struct _LONG_ADDR
{
BYTE v[8];
} LONG_ADDR;

typedef struct _SHORT_ADDR
{
struct _SHORT_ADDR_bits
{
BYTE MSB;
BYTE LSB;
} byte;
WORD Val;
BYTE v[2];
} SHORT_ADDR;

typedef union _ADDR
{
LONG_ADDR LongAddr;
SHORT_ADDR ShortAddr;

BYTE v[8];
} ADDR;

+1
0
-1
March 30, 2007 - 2:51pm
Guest

could it be
you are making a union of two identical entities

LONG_ADDR contains BYTE v[8]; and you repeat that in the union, just as a test try removing BYTE v[8]; from the union, if that works, experiment from there.

Erik

+1
0
-1
March 30, 2007 - 4:29pm
Guest

Hi erikm thanks for yours proporsals!

I think that I have found the solution, the problem is not the definition of the "typedef struct" inside of another type struct. The problem is that I am calling the #include "file.h" where the LONG_ADDR,SHORT_ADDR and ADDR are defined twice. I have made the experiment only with the main with only #include "file.h" and nothing more and everthing was rigth, no errors. But after I tryed put the sentence #include "file.h" twice

#include "file.h"
#include "file.h"
void main(void)
{}

then the error appears.
My program have some files and maybe I am calling #include "file.h" in two diferent files and this files are called from the main too.

+1
0
-1
March 30, 2007 - 7:12pm
Guest

have you tried what many do (I do not)

for every .h file you write

#ifndef HFILENAME
#define HFILENAME

the guts of the file

#endif

That way any .h file will only be included once, however you make your code

+1
0
-1
April 1, 2007 - 11:32am
Guest

Thanks again Erikm!!

I usually do this (put #ifndef HFILENAME ....) with my "xxx.h" files but this time I didn't and I think that will save me time.

Thanks

Santi

+1
0
-1
April 2, 2007 - 3:59pm
Guest

The one thing we all have been bitten by several time is "I usually do this ... but this time I didn't ".

When some habit is ingrained and you skip it, you never think that the reason you see something is because of xxx, because you 'always' make xxx impossible.

Erik