|
|
View previous topic :: View next topic |
Author |
Message |
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
Compiler error |
Posted: Fri May 20, 2005 9:37 am |
|
|
Float Vbat;
int floatgrootte = sizeof(Vbat);
? Why do I get an error ?
I want to get the size of a float (should return '4' bytes)
thx |
|
|
valemike Guest
|
|
Posted: Fri May 20, 2005 12:04 pm |
|
|
I'm pretty sure that
sizeof(float) will work.
or if you have
struct mystruct
{
...
...
};
you can do a sizeof(mystruct) i think.
i guess its a c syntax thing. since i don't have a k&r book in front of me, i can't tell you for sure. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 20, 2005 12:47 pm |
|
|
Christophe, the answer is "go simple", and "make a test program".
The following program displays this result: 4
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void main()
{
int floatgrootte;
float Vbat;
floatgrootte = sizeof(Vbat);
printf("%d", floatgrootte);
while(1);
} |
|
|
|
Ttelmah Guest
|
|
Posted: Fri May 20, 2005 2:18 pm |
|
|
I think the problem is between the difference in 'runtime' code, and 'compile time' code. Sizeof(Vbat), requires that the variable 'Vbat' actually exists (which is not true at 'compile time'), while 'Sizeof(float)', only requires the understanding of the data type. Sizeof(Vbat), will work fine at runtime (as PCM programmer points out).
So the two choices are:
1)
Code: |
Float Vbat;
int floatgrootte = sizeof(Float);
|
or 2):
Code: |
Float Vbat;
int floatgrootte;
floatgroote = sizeof(Vbat);
|
The second evaluates 'sizeof', at runtime, rather than at compile time, and again will work.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|