Pages: [1]   Go Down
Author Topic: const + struct + array  (Read 936 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19367
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19367
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1287
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19367
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;
« Last Edit: July 30, 2012, 12:06:33 am by lloyddean » Logged

Pages: [1]   Go Up
Jump to: