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 support@ccsinfo.com

Initializing nested structures...

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



Joined: 29 Mar 2009
Posts: 2

View user's profile Send private message

Initializing nested structures...
PostPosted: Sun Mar 29, 2009 4:54 pm     Reply with quote

Hi,

I'm just getting to grips with the CCS PIC compiler, so please go easy on me Smile

I have a structure, within a structure that I'd like to initialize. Whilst the code below compiles cleanly under, say gcc - I can't get the CCS PIC compiler to compile it - it just halts with a "Expression must evaluate to a constant" error...

Any chance someone can give the code a quick look over, and see what's likely to be the problem?

Cheers

-Tacks

---

Code:

#include <16F819.h>

struct Values{
  int8  name;
  int8  value;
};

struct Pairs{
  int8  option;
  int8  value;
};


struct Test {
  int16  my_code;
  struct Pairs     config[ 8 ];
  struct Values  current[ 8 ];
};

struct Test my_test = {

  0x1234,

  {
    { 0x01, 0x02 },  <--- errors at the start of this line.
    { 0x03, 0x04 },
    { 0x04, 0x05 },
    { 0x06, 0x07 }
  },

  {
    { 0x11, 0x12 },
    { 0x13, 0x14 },
    { 0x14, 0x15 },
    { 0x16, 0x17 }
  }

};

void main(){
  /* Dummy */
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 29, 2009 5:47 pm     Reply with quote

It works in vs. 4.089 and 4.090. Here's the output. The last 4 sets in
each group are zeros because your code declared an array of 8 sets,
but only the first 4 are initialized.
Quote:

my_test.my_code = 1234

01 02
03 04
04 05
06 07
00 00
00 00
00 00
00 00

11 12
13 14
14 15
16 17
00 00
00 00
00 00
00 00



The program below was tested in the MPLAB simulator, using "UART1"
(a feature of the simulator) to display the output in the Output Window.
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)

struct Values{
  int8  name;
  int8  value;
};

struct Pairs{
  int8  option;
  int8  value;
};


struct Test {
  int16  my_code;
  struct Pairs  config[ 8 ];
  struct Values current[ 8 ];
};

struct Test my_test = {
  0x1234,
  {
    { 0x01, 0x02 },   
    { 0x03, 0x04 },
    { 0x04, 0x05 },
    { 0x06, 0x07 }
  },

  {
    { 0x11, 0x12 },
    { 0x13, 0x14 },
    { 0x14, 0x15 },
    { 0x16, 0x17 }
  }

};


//==========================
void main()
{
int i;

printf("my_test.my_code = %lx \n\r", my_test.my_code);

for(i=0; i<8; i++)
    printf("%x %x \r", my_test.config[i].option, my_test.config[i].value);

printf("\n\r");

for(i=0; i<8; i++)
    printf("%x %x \r", my_test.current[i].name, my_test.current[i].value);


while(1);
}
Tackyone



Joined: 29 Mar 2009
Posts: 2

View user's profile Send private message

PostPosted: Mon Mar 30, 2009 3:06 am     Reply with quote

Hi,

Thanks for that - I'll check what version we're running here...

Great to know it's not me going mad Smile

Thanks,

-Tacks
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