Go Down

Topic: const + struct + array (Read 974 times) previous topic - next topic

Jose Francisco


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.

Nick Gammon


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 hardware & library 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.


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;

   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