View previous topic :: View next topic |
Author |
Message |
Guest Guest
|
Dumb-struct |
Posted: Mon Jun 14, 2004 9:43 am |
|
|
Searched on "struct initialize" and didn't find much.
I tried to clean up a project (PCM 3.185 on a PIC16F877) that works fine. Wanted to put some of my constants in structures like:
struct axisconsts
{
float offset;
float sense;
} x_axis, y_axis;
x_axis.offset = 3.1459;
x_axis.sense = 2.71828;
.... and so forth.
The compiler complains that the line with x_axis.offset needs a "(". This is right out of the book, basically. What is wrong? Take out the struct initialization lines and it compiles fine.
Thanks. |
|
|
LloydSargent
Joined: 11 Jun 2004 Posts: 13
|
|
Posted: Mon Jun 14, 2004 9:57 am |
|
|
Hmm...
I tried it and it worked fine (not the answer you want to hear!).
Where do you have
x_axis.offset = 3.1459;
x_axis.sense = 2.71828;
In your code?
Cheers,
Lloyd |
|
|
SteveS
Joined: 27 Oct 2003 Posts: 126
|
|
Posted: Mon Jun 14, 2004 9:57 am |
|
|
You need to put the initializations inside a function or main.
-or initialize them like this (based on K&R)-
Code: |
struct axisconsts
{
float offset;
float sense;
};
struct axisconsts x_axis = {3.1459 , 2.789 };
struct axisconsts y_axis = {1.23, 4.56 };
|
- SteveS |
|
|
LloydSargent
Joined: 11 Jun 2004 Posts: 13
|
|
Posted: Mon Jun 14, 2004 10:09 am |
|
|
Okay, before you get too confused, HERE are the only valid ways to initialize the struct.
1ST
Code: |
struct axisconsts
{
float offset;
float sense;
};
struct axisconsts x_axis= {3.1459, 2.71828};
main()
{
}
|
2ND
Code: |
struct axisconsts
{
float offset;
float sense;
} x_axis, y_axis;
main()
{
x_axis.offset = 3.1459;
x_axis.sense = 2.71828;
}
|
3RD
Code: |
main()
{
struct axisconsts
{
float offset;
float sense;
} x_axis, y_axis;
x_axis.offset = 3.1459;
x_axis.sense = 2.71828;
}
|
4th
Code: |
struct axisconsts
{
float offset;
float sense;
} x_axis= {3.1459, 2.71828};
main()
{
}
|
This is off the top of my head. I may have missed one...
Cheers,
Lloyd |
|
|
Guest Guest
|
Struct initialization |
Posted: Mon Jun 14, 2004 10:39 am |
|
|
OK folks -
Thanks very much and let me try some of these suggestions.
Michael |
|
|
|