Trying to understand Const and Struct

Hey all,
Just trying to figure out Const and Struct. The reason for this is that I want to convert a sample code to regular AVR C language for the AVR-GCC.

But I can't get it to work. The original code is the following, for the Arduino;

/* Analog readings corresponding to different sensor states */
struct input_ranges {
  int sensor_state;
  const String label;
  int range_bottom;
  int range_optimum;
  int range_top;
};
const input_ranges ranges[] {
  { STATE_SHORT,        "Shorted",         0,    0,  162 },  // 0
  { STATE_NORMAL,       "Normal",        163,  326,  409 },  // 1
  { STATE_TAMPER,       "Tamper",        410,  495,  551 },  // 2
  { STATE_ALARM,        "Alarm",         552,  609,  641 },  // 3
  { STATE_ALARM_TAMPER, "Alarm+Tamper",  642,  675,  848 },  // 4
  { STATE_CUT,          "Cut",           849, 1023, 1023 },  // 5
  { STATE_UNKNOWN,      "Unknown",      1024, 1024, 1024 },  // 6. Range beyond analog in because this is never read
};

This code sample is from the Freetronics blog for a security shield.

When pasting that in AtmelStudio it says the following;

X1 - unknown type name 'String'
X2 - unknown type name 'input_ranges'
X3 - expected '=', ',', ';', 'asm' or 'attribute' before '{' token

I'm no professional in C programming, so I might very well be doing it wrong. But, I've had various success with replacing "String" for char so I did that here, seems to work ok.

But then the X2 error, I guess it should be any sort of type such as "char" or so, but I don't know which. It feels like it should "connect" to the struct above it somehow, as it uses the same name "input_ranges". Not sure if thats correct?

Then the last one is the most troublesome.. What kind of "function"/listing/array? or what its called, is this supposed to be?

{ SENSOR_STATE_SHORT,        "Shorted",         0,    0,  162 }

It feels like an array, but as I learned some years ago.. Arrays aren't what I used to think of them, coming from other languages where it works quite a different way.. So ok, but it feels like some sort of an array.

And the errors tells me that it's now allowed inside the ranges - array? I guess its 7 arrays inside the "ranges" array, correct?

So is it possible to convert this to regular C and could someone point me in the direction or even tell me exactly which functions/types I need for this?

I know that Arduino has some additional functionality compared to just standard C, so I'm up for it if I need to include an additional function or so, just to get this to work.

But as it stands right now, I have no idea where to look further.. Please help if you can! :slight_smile:
Thanks!

unknown type name 'String'

Did you remember to include WString.h?

So is it possible to convert this to regular C

String is a class, so no.

However, you could substitute a more appropriate C string.

Thanks for your reply AWOL! :slight_smile:

Not sure if I understood you correctly.. But is the following;

const input_ranges ranges[] {
  { STATE_SHORT,        "Shorted",         0,    0,  162 },  // 0
  { STATE_NORMAL,       "Normal",        163,  326,  409 },  // 1
  { STATE_TAMPER,       "Tamper",        410,  495,  551 },  // 2
  { STATE_ALARM,        "Alarm",         552,  609,  641 },  // 3
  { STATE_ALARM_TAMPER, "Alarm+Tamper",  642,  675,  848 },  // 4
  { STATE_CUT,          "Cut",           849, 1023, 1023 },  // 5
  { STATE_UNKNOWN,      "Unknown",      1024, 1024, 1024 },  // 6. Range beyond analog in because this is never read
};

part of WString and the String class?

Otherwise it feels like I shouldn't have to use the String class or the WString files..?
I mean, I can change the line with "const String label;" and the values "Shorted", "Normal" and so on with numbers or anything else..

I mean, that's really not the problem.

The problem is that this;

{ STATE_SHORT,        "Shorted",         0,    0,  162 },  // 0

Isnt recognized as correct code inside;

const input_ranges ranges[] { }

I just want that to work, unless that's a function of the String class, which I don't know and I cant find any reference to it inside the WString.h file.. But unless it is, I shouldnt have to include the WString files right?

So, if WString only is for the possibility to have the "text" inside the array

{ STATE_SHORT,        "Shorted",         0,    0,  162 }

Then I will ditch it.. But removing String and the "Shorted" part doesn't seem to work so.. :frowning:

chrisfredriksson:
So, if WString only is for the possibility to have the "text" inside the array

No. In the C programming language you will use char arrays and pointers to char arrays, of course.

But syntax does matter.

struct input_ranges {
  int sensor_state;
  char* label;
  int range_bottom;
  int range_optimum;
  int range_top;
};

const input_ranges ranges[]= {
  { STATE_SHORT,        "Shorted",         0,    0,  162 },  // 0
  { STATE_NORMAL,       "Normal",        163,  326,  409 },  // 1
  { STATE_TAMPER,       "Tamper",        410,  495,  551 },  // 2
  { STATE_ALARM,        "Alarm",         552,  609,  641 },  // 3
  { STATE_ALARM_TAMPER, "Alarm+Tamper",  642,  675,  848 },  // 4
  { STATE_CUT,          "Cut",           849, 1023, 1023 },  // 5
  { STATE_UNKNOWN,      "Unknown",      1024, 1024, 1024 },  // 6. Range beyond analog in because this is never read
};