473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Initiate a union

I want to initiate a union and and have the problem that I sometimes
need to use float and sometimes an int. The float values (u.fval) turns
up ok but not the ints (u.ival).

I tried typecating it as below but it does not seem to do the trick.

Would be happy if anyone has a solution for this problem.

struct str {
int i;
float f;

typedef union {
float fval;
int ival;
} u;

} tStruct;

tStruct structArray[] = {
{ 0, 0.0 , 3.2},
{ 0, 0.0 , (float)(int)9}
};

Nov 15 '05 #1
6 1366
martin wrote:
I want to initiate a union and and have the problem that I sometimes
need to use float and sometimes an int. The float values (u.fval) turns
up ok but not the ints (u.ival).

I tried typecating it as below but it does not seem to do the trick.

Would be happy if anyone has a solution for this problem.

struct str {
int i;
float f;

typedef union {
float fval;
int ival;
} u;

} tStruct;
The above is hopelessly mangled. See the example below.
tStruct structArray[] = {
{ 0, 0.0 , 3.2},
{ 0, 0.0 , (float)(int)9}
};


Here is a C99-oriented way to do this:
#include <stdio.h>
typedef struct
{
int i;
float f;
union
{
float fval;
int ival;
} u;

} tStruct;
int main(void)
{
tStruct structArray[] = {
{.u.fval = 3.2},
{.u.ival = 9}
};
printf("structArray[0].u.fval contains %g, expected 3.2\n",
structArray[0].u.fval);
printf("structArray[1].u.ival contains %d, expected 9\n",
structArray[1].u.ival);
return 0;
}
[output]
structArray[0].u.fval contains 3.2, expected 3.2
structArray[1].u.ival contains 9, expected 9
Nov 15 '05 #2

If we want to initialize i, f and union u ?
How to do that

Nov 15 '05 #3

gladiator 写道:
If we want to initialize i, f and union u ?
How to do that

you can do it like this
tStruct structArray[] = {
{1,2.3,.u.fval = 3.2},
{2,3,4,.u.ival = 9}
};
i have test it!

Nov 15 '05 #4
Thanks for the quick response but when I tried your solution my
compiler gave me the following error:

Serious error: <expression> expected but found '.'

Nov 15 '05 #5
martin wrote:
Thanks for the quick response but when I tried your solution my
compiler gave me the following error:


See below. You also failed to show us what you actually tried. We have
not idea what didn't work.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #6
In article <11**********************@o13g2000cwo.googlegroups .com>,
us******@gmail.com <us******@gmail.com> wrote:
gladiator =E5=86=99=E9=81=93=EF=BC=9A
If we want to initialize i, f and union u ?
How to do that

you can do it like this
tStruct structArray[] = {
{1,2.3,.u.fval = 3.2},
{2,3,4,.u.ival = 9}
};
i have test it!


You didn't test it with a C89 compiler.
--
Camera manufacturers have temporarily delayed introduction of
sub-millibarn resolution bio-hyperdimensional plasmatic space polyimaging,
but indications are that is still just around the corner.
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
0
by: Bazza Formez | last post by:
Hi, Can anyone suggest a technical solution to this tricky business requirement.... I am developing an online ordering system using ASP.NET. (Orders are entered online from the web by...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
4
by: Hamayun Khan | last post by:
Hi I have Install Web content extractor on my pc. Web Content Extractor exe has the following path C:\Program Files\Web Content Extractor\WCExtractor.exe Now I used cmd file to initiate Web...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.