CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

extern and union

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

extern and union
PostPosted: Wed Feb 27, 2013 6:15 am     Reply with quote

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
Code:
extern union rcv;

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: 19535

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 6:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 6:37 am     Reply with quote

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: 19535

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 8:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 27, 2013 10:39 am     Reply with quote

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 Smile
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Thu May 02, 2013 9:11 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group