Go Down

Topic: const + struct + array (Read 1 time) previous topic - next topic

Jose Francisco

Hello.

I have N_SLAVES in my project and I want to store its configuration in a array of structures.
That configuration will not be variable in run time.

This is the code I'm trying:

Code: [Select]

#define N_SLAVES 1

typedef struct STATIONS {
  int a;
  int b;
  float c;
};

const STATIONS station[N_SLAVES];
station[0] = (STATIONS) {1,2,3.1};



file.cpp:43:32: error: uninitialized const 'station'
file.cpp:44:1: error: 'station' does not name a type

Can you help me?
Thank you

Nick Gammon

Code: [Select]
sketch_jul29b:8: error: uninitialized const 'station'
sketch_jul29b:9: error: expected constructor, destructor, or type conversion before '=' token


Your code did not produce what you claimed it did.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

marco_c

I think the typedef is the issue. You need to declare it in a header file for it to work. Seemed to fix a similar issue I had in the past.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Nick Gammon

Code: [Select]
const STATIONS station[N_SLAVES];
station[0] = (STATIONS) {1,2,3.1};


You can't assign to a constant, and you can't assign like that anyway.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

lloyddean

#5
Jul 29, 2012, 07:13 pm Last Edit: Jul 30, 2012, 07:06 am by lloyddean Reason: 1
Code: [Select]

const size_t    N_SLAVES    = 3;

struct STATIONS
{
   int     a;
   int     b;
   float   c;
};

const STATIONS station[N_SLAVES] = {
     { 1, 2, 3.1f }        // station[0]
   , { 1, 2, 3.1f }        // station[1]
   , { 1, 2, 3.1f }        // station[2]
};

int     a = station[0].a;
int     b = station[0].b;
float   c = station[0].c;


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy