Casting function parameters C / C++

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Casting function parameters C / C++

1,460 Views
Ricardo_RauppV
Contributor I

Hy guys

I´m using  CW6.2 C++ in a QE128 design, wich is about my post here...

I also use CW 7.1 for ColdFires.

In ColdFires IDE I can cast a parameter in order it matches the expected struct, without the need of build such struct to pass it.

 

Example:

 

- THE STRUCT 

struct rtc_t

{

   uint8 day,month,year;

}

 

- THE FUNCTION

void cpu_rtc::write_rtc( rtc_t data )

{

   rtc_cpu.day=data.day;

   rtc_cpu.month=data.month;

   rtc_cpu.year=data.year;

}

 

- THE CALLING

1) This works: 

 

    rtc_t r={1,2,3};// init var rtc_t type

    write_rtc( r);  // pass expected type to the function

 

2) This ( the way I want ) doesn´t work: CW complains about wrong cast. 

 

     write_rtc( (rtc_t) {26,10,9});

 

It works in CW7.1 for ColdFires ( pure C ...I didn´t try C++ yet)

Its very practical, once we don´t need to formally create a struct to use as parameter.

 

I could not make it work in CW6.2 using C++ (maybe it dodesn´t work even in C.

 

Did I forget some small detail to accomplish it? 

Has anyone some experience like that?

 

Thanks everybody !!

 

Ricardo Raupp 

Labels (1)
Tags (1)
0 Kudos
Reply
3 Replies

590 Views
CompilerGuru
NXP Employee
NXP Employee

That's a C99 feature called compound literals. The HC08 compiler does not support this C99 extension while the CF compiler does support it.

For now you will have to create local structs or pass the individual members separately into the function.

 

Daniel

0 Kudos
Reply

590 Views
Ricardo_RauppV
Contributor I

Daniel

I spent a good time trying to cast it by several ways....

Can we ask Freescale to add it to CW?

 

Very thanks for your help..

Ricardo Raupp

0 Kudos
Reply

590 Views
Lundin
Senior Contributor IV
That code isn't "pure C", but as mentioned it is C99, which is a standard that hasn't received a wider acceptance in the industry. The industry de facto standard is still "C90" (ISO 9899:1990).

Therefore you probably don't want to use C99 in embedded apps, because of portability reasons. There are very few compilers supporting it. Sticking to C++ might be better if you are unsure of the difference between C90 and C99.

Also, as a rule of thumb you should never pass structs by value. It turns the code inefficient for no reason. You might think there are only a few bytes passed, but the compiler is free to add additional padding bytes to a struct/union. This is especially poor practice on microcontrollers with limited RAM and stack space.
0 Kudos
Reply