By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,507 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,507 IT Pros & Developers. It's quick & easy.

declaring union

P: 21
how do you declare a union with a defined union?
Nov 5 '08 #1
Share this Question
Share on Google+
8 Replies


Expert 10K+
P: 11,448
Say what?

kind regards,

Jos
Nov 5 '08 #2

P: 21
lollllllz

well i was supposed to write a code which i have written but the last statement in the exercise was something i didnt understand .... and that is

Declare another union of the same constitution using the previously defined Union.
Nov 5 '08 #3

P: 21
I was supposed to define a union as a datatype suppose i gave it a name as store now how am i suppose to declare another union with this named union??
Nov 5 '08 #4

Banfa
Expert Mod 5K+
P: 8,916
You mean you did something like this

Expand|Select|Wrap|Line Numbers
  1. struct MyStruct {
  2.    int SomeData;
  3. } StructInstance1;
  4.  
To declare another data store all I need to do is

Expand|Select|Wrap|Line Numbers
  1. struct MyStruct StructInstance2;
  2.  
Only of course you would need to use unions.
Nov 5 '08 #5

P: 21
oh ok ...... thanks!
Nov 5 '08 #6

P: 21
ok so if i write it like this

#include <stdio.h>

typedef union
{
int a;
char b;
}data;
union data data1;


is it fine?

the ques was actually this

Define a union that can store both char and int type data values. You need to define that in a way such that later, the union name can be used as a data type itself. Declare another union of the same constitution using the previously defined Union.

Sorry but m totally new to C and home based student so having a lot of problem studying myself ..... sorry for asking too many ques
Nov 5 '08 #7

100+
P: 424
define that in a way such that later, the union name can be used as a data type itself.
The way I read it, this means you should be able to declare another union using a single type specifier. In other words, use a typedef just like you did and then you can declare more unions like this:

data unioninstance

(because of the typedef, "data" can be used as a type in itself and doesn't need to be preceded by the "union" keyword.)
Nov 5 '08 #8

P: 21
thanks ! :) thanks for all your help..
Nov 5 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.