0

first time posting here

I'm currently porting some code for an embedded device. Basically getting everything to work with a new compiler (AVR-GCC) from an out-of-date existing proprietary compiler

I've come across this strange looking (to me anyway!) variable in a struct. I can't work out what it is with the parentheses. It is in a struct which is used for raw values:

float   (*tc)( float value );

My IDE highlights 'value' as a compiler keyword, just like 'float' so I don't know if this is AVR-GCC specific?

This is then used in a function which has a float argument called 'reading' and it tries to return the following:

    return (raw[rCN3].tc)( reading );

The line above actually causes the program to attempt to access out of bounds memory.

I haven't seen code like this before so was wondering if anyone could help me decipher it? It worked with the old compiler but is causing a problem with AVR-GCC

Thanks in advance. Alex

4 Answers4

1

This is a function pointer. It points to a function that returns a float value and that has a float parameter.

QI3it
  • 181
  • 7
1

Two things:

1) float (*tc)( float value ) is a function pointer to a function taking a float as a parameter, returning a float

2) 'value' is a keyword in C# and may also be in other languages; hence its highlighting. Check your editor language settings.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

That is a function pointer variable.

tc is a pointer to a function, which takes a single float as argument and returns a `float.

The reason it accesses out of bounds memory is probably because rCN3 is out of bounds for the array raw.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks! I should really have spotted this....haven't used function pointers in quite a long time. Thanks for prompt response – mr_Alex_Nok_ May 29 '13 at 10:28
0

That is a function pointer.

It means that tc holds the address of a function accepting a single float argument, and returning a float value.

For instance, you could set it to the standard library's sinf function, like so:

somestruct.tc = sinf;

somestruct.tc(3.14159265f / 2);  /* This will return roughly 1.0f. */

Also, the ever-useful cdecl says:

declare tc as pointer to function (float) returning float

unwind
  • 391,730
  • 64
  • 469
  • 606