Saturday, May 22, 2010

Basic C syntax, assigning a value?

I am working with Borland C++ 5.02 compiling a C program to read an INI file with this function prototype:





extern const double readIniDouble( void *fp, const char *section, const char *name, const double defValue );





I can read the value with no problem with this code:





double test;


test = readIniDouble( iniFile, "ASection", "AName", 2 );


cprintf( "%e", test ); // Correctly writes the default value of '2'





The problem is that I need to get the value into a struct:





static struct settings tmp_set;


tmp_set.aSetting = readIniDouble( iniFile, "ASection", "AName", 2 );


cprintf( "%e", %26amp;tmp_set.aSetting ); // does not work





This compiles but prints an unexpected large number. If I use an ampersand or asterisk in front of tmp_set on the left side of the assignment (%26amp;tmp_set.aSetting = ... ) I get a compiler error 'Illegal use of floating point'.





What is the correct syntax to assign the return value of the iniReadDouble() function directly to the struct member?

Basic C syntax, assigning a value?
don't print %26amp;tmp_set.aSetting, just tmp_set.aSetting. With the %26amp;, you're taking the memory address of that variable, not its value.


No comments:

Post a Comment