C question - floating point initialization

Status
Not open for further replies.

LBdgWgt

Member level 5
Joined
Mar 6, 2006
Messages
83
Helped
5
Reputation
10
Reaction score
0
Trophy points
1,286
Activity points
2,066
Hello,

For example i have this type of union data:

typedef union {
unsigned long int my_long;
float my_float;
double my_double;
unsigned int my_word[2];
unsigned char my_byte[4];
} mydata;

how can i initialize a variable declared in mydata type with floating point value?
I am writing a code for HCS12 using ICC12
this code line doesn't work when i chek in the .lst file

mydata data1 = {1.25};

results in:

0404 _data::
0404 00000001 .word 0,1

is this the problem of the compiler or...?
thanks for any answer.
 

You would have to have a constant data variables in order to be able to initialize them.

Yuo need a const union in rom
 

actually this data will be implemented inside EEPROM (not Flash program memory), so i want to generate a floating point initialization value.

so what should i do?
 

If you put a value in the float variable in your union, all the variables in the union would get the same value.

You need a rom struct

const rom struct {
unsigned long int my_long;
float my_float;
double my_double;
unsigned int my_word[2];
unsigned char my_byte[4];
} mydata;

initialize all the variables to what you want.
bu you won't be able to change them in the code run time, they would be constant

Added after 5 minutes:

even a better way:

declare your structure first:

struct mydataStruct
{
unsigned long int my_long;
float my_float;
double my_double;
unsigned int my_word[2];
unsigned char my_byte[4];
}

then...

initialise it:

const rom struct mydataStruct mydata[5] = { put values here separated by a comma };
 

To initialize a variable declared in mydata type with floating point value you must use this:

mydata data1.my_float = 1.25;

Regards, svicent
 

svicent said:
To initialize a variable declared in mydata type with floating point value you must use this:

mydata data1.my_float = 1.25;

Regards, svicent


That is not an initialization.
That is a statement.
 

In "The C programming language", you can read:

"A union may only be initialized with a value of the type of its first member. "

This code line works OK

mydata data1 = {1.25};

if you define:

typedef union {
float my_float;
unsigned long int my_long;
double my_double;
unsigned int my_word[2];
unsigned char my_byte[4];
} mydata;

Regards, svicent
 

    LBdgWgt

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…