CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Dumb-struct

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest
Guest







Dumb-struct
PostPosted: Mon Jun 14, 2004 9:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 14, 2004 9:57 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 14, 2004 9:57 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Jun 14, 2004 10:09 am     Reply with quote

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
PostPosted: Mon Jun 14, 2004 10:39 am     Reply with quote

OK folks -

Thanks very much and let me try some of these suggestions.

Michael
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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