Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > union

Reply
Thread Tools

union

 
 
newbai
Guest
Posts: n/a
 
      03-08-2007
union
{
int ival;
char ival;
}u;
main()
{
u={1,'2'};
printf("%d",sizeof(u));
}

can someonw help me in knowing how to do this?

 
Reply With Quote
 
 
 
 
Sheth Raxit
Guest
Posts: n/a
 
      03-08-2007
On Mar 8, 12:06 pm, "newbai" <siddhakudchad...@gmail.com> wrote:
> union
> {
> int ival;
> char ival;}u;

you can not have two variable with same name in same scope.
>
> main()
> {
> u={1,'2'};
> printf("%d",sizeof(u));
>
> }
>
> can someonw help me in knowing how to do this?



 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      03-08-2007
On 7 Mar 2007 23:06:53 -0800, "newbai" <>
wrote:

>union
>{
>int ival;
>char ival;
>}u;
>main()
>{
>u={1,'2'};
>printf("%d",sizeof(u));
>}
>
>can someonw help me in knowing how to do this?


Do what? You need to explain what you want and how what you have
differs.

Here is a starting hint. A union has only one value. In your case
you can chose whether to assign a value to the int or the char. Of
course, you need to give them different names.


Remove del for email
 
Reply With Quote
 
HelloLinux
Guest
Posts: n/a
 
      03-08-2007
I don't know what your question is, but I guess code below mey be what
you need.

#include <stdio.h>
#include <stdlib.h>
union
{
int ival;
char cval;
}u;

int main(void)
{
u.ival=1;
printf("%d",sizeof(u));
return 0;
}

"newbai" <siddhakudchad...@gmail.com> wrote:
> union
> {
> int ival;
> char ival;}u;
>
> main()
> {
> u={1,'2'};
> printf("%d",sizeof(u));
>
> }
>
> can someonw help me in knowing how to do this?



 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-08-2007
HelloLinux said:

> I don't know what your question is, but I guess code below mey be what
> you need.


I doubt it very much. Why would he need incorrect code?

sizeof yields a size_t, not an int, so %d is not an appropriate printf
format specifier for it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Doug
Guest
Posts: n/a
 
      03-08-2007

Richard Heathfield wrote:
> HelloLinux said:
>
> > I don't know what your question is, but I guess code below mey be what
> > you need.

>
> I doubt it very much. Why would he need incorrect code?
>
> sizeof yields a size_t, not an int, so %d is not an appropriate printf
> format specifier for it.


I'll probably get flamed to hell and back for this, but what the
heck. I used to enjoy taking part in these groups but now I can't
stand the constant nitpicking and snide remarks.

Richard, everything you said was completely correct. Couldn't you
have found a nicer way to say it?

HelloLinux took a guess at what the OP was asking and probably gave
him enough to get going again. I don't see that his help was
completely worthless because he used an inappropriate printf format
specifier.

I just think a comment like "that's probably what you need, but note
that a size_t is not an int, so %d is not an appropriate printf
specifier for it" would have been better. If you actually want to be
helpful, you might even suggest how to solve that (like Keith did).

Doug

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-08-2007
Doug said:

<snip>

> Richard, everything you said was completely correct. Couldn't you
> have found a nicer way to say it?


Yes, I probably could. My apologies to HelloLinux and to you.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-08-2007
newbai wrote:
> union
> {
> int ival;
> char ival;


You cannot have two identifiers with the same sequence of significant
characters, within the same scope. Note though that labels are in a
separate namespace from types and objects, but it's still a bad idea
to use two identifiers with the same name twice in a block.

> }u;
> main()
> {
> u={1,'2'};


You can only assign to a single member of a union at a time. Though a
union may have many members, only one member can be used at a time.
That's because all the members of the union physically overlap in
storage.

> printf("%d",sizeof(u));


Use the %lu format specifier to print values yielded by the sizeof
operator. Be sure to cast the corresponding argument to unsigned long.
Also if you're sure your program will only run under C99 supported
implementations, you can use the %zu format specifier, specifically
meant for size_t arguments.

> }
>
> can someonw help me in knowing how to do this?


Try this version of your program:

#include <stdio.h>

union test{
int i;
char c;
};

int main(void) {
union test u;

u.i = 1;
printf("u.i == %d\n", u.i);
u.c = '@';
printf("u.c == %c\n", u.c);
printf("sizeof u == %lu\n", (unsigned long int) sizeof u);

return 0;
}

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-08-2007
On 7 Mar 2007 23:06:53 -0800, in comp.lang.c , "newbai"
<> wrote:

>union
>{
>int ival;
>char ival;


union members must have different names

>}u;


>can someonw help me in knowing how to do this?


You need to explain what you want to do....


The union you define above can hold EITHER an int or a char, but not
both. If you want to assign a value to it you use

u.ival = 1;
or
u.chval = '2';

but not both.

To hold multiple sets of data, use a struct.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Reply With Quote
 
Doug
Guest
Posts: n/a
 
      03-09-2007
On Mar 8, 6:21 am, Richard Heathfield <r...@see.sig.invalid> wrote:
> Doug said:
>
> <snip>
>
> > Richard, everything you said was completely correct. Couldn't you
> > have found a nicer way to say it?

>
> Yes, I probably could. My apologies to HelloLinux and to you.
>
> --
> Richard Heathfield
> "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
> email: rjh at the above domain, - www.


No worries. I should probably apologise for sticking my nose in. I'd
had a hard day, sorry.

Doug

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL UNION query in Java goli Java 4 04-05-2005 09:54 AM
SQL UNION -like filtering of offline DataSet into DataView for WebControl binding Marco Ippolito ASP .Net 2 05-19-2004 12:35 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
THE STATE OF THE UNION Jenna Bush MCSE 95 01-30-2004 02:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57