View previous topic :: View next topic |
Author |
Message |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
extern and union |
Posted: Wed Feb 27, 2013 6:15 am |
|
|
hi guys;
I'm trying to define a union in main.c file and use it on another c file in Same project using extern key word,
but compiler fail to compile.
main.c
Code: | union rcv{
char chr_array[8];
unsigned int16 int16_array[4];
unsigned int32 int32_array[2];
}; |
interruptc.c
Compiler cannot compile it and gives me following error:
error33"E:\Interrupts.c" Line7(23,31):Expecting a {
Can someone please tell me how to use union with extern keyword properly.
My pic is 16F628A.
compiler version is 4.084
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Feb 27, 2013 6:27 am |
|
|
Problem is that you haven't got a variable called rcv, just a union declaration for it. Extern applies to a variable declaration. You need a variable name in the extern declaration. I'd suggest typedefing the union declaration, and using this to declare the variable.
Best Wishes |
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Wed Feb 27, 2013 6:37 am |
|
|
Ttelmah wrote: | Problem is that you haven't got a variable called rcv, just a union declaration for it. Extern applies to a variable declaration. You need a variable name in the extern declaration. I'd suggest typedefing the union declaration, and using this to declare the variable.
Best Wishes |
Can you please show me how to do it?
This is the first time I'm using union or typedef.
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Wed Feb 27, 2013 8:27 am |
|
|
Comments inline:
Code: |
union rcv{
char chr_array[8];
unsigned int16 int16_array[4];
unsigned int32 int32_array[2];
};
//This is the union 'definition'. It says I now have the ability to declare a
//union defined as rcv, but does not declare any variable.....
union rcv fred;
//This now declares the variable 'fred' to be a union, as declared by rcv.
//or for extern,
extern union rcv fred;
//tidier though
typedef union {
char chr_array[8];
unsigned int16 int16_array[4];
unsigned int32 int32_array[2];
} joiner;
//now declares a new 'type', called 'joiner', which you can use to declare
//a variable.
//So:
joiner rcv;
//declares 'rcv' as a union of this type.
extern joiner rcv;
//declares the external variable rcv as a union
|
Best Wishes |
|
|
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Wed Feb 27, 2013 10:39 am |
|
|
Ttelmah wrote: | Comments inline:
Code: |
union rcv{
char chr_array[8];
unsigned int16 int16_array[4];
unsigned int32 int32_array[2];
};
//This is the union 'definition'. It says I now have the ability to declare a
//union defined as rcv, but does not declare any variable.....
//declares the external variable rcv as a union
|
Best Wishes |
Thank you very much Ttelmah. I'll give it a try |
|
|
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Thu May 02, 2013 9:11 am |
|
|
Hi Ttelmah,
I need to do a similar thing, but am struggling with the concept. Basically I have a 32bit 'extern' variable (ival2), that I need byte access to. Usually I would use:
Code: |
union {
int32 ival2;
int8 bytes[4];
}minvalue;
//minvalue.ival2 = 32bit number;The bytes can now be accessed as:
//minvalue.bytes[0], ..., minvalue.bytes[3]
|
But how do I do it with the extern? because I cannot use:
Code: | union {
extern int32 ival2;
int8 bytes[4];
}minvalue;
//minvalue.ival2 = 32bit number;The bytes can now be accessed as:
//minvalue.bytes[0], ..., minvalue.bytes[3]
|
Any help much appreciated.
Carl |
|
|
|