'Problem with Arduino using arrays in Structure
With an Arduino Uno I'm having a problem that is probably simple to solve for somebody who knows what they're doing. I'm trying to get this code to print out "-30" but all I get is "0" no matter what value I put in "setpt[x]" in the serial.print line in loop().
Thanks in advance, Don
typedef struct
{
byte seg_num[12];
byte ramp_hrs[12];
int setpt[12];
byte ramp_mins[12];
}record_type;
record_type profile[8];
void setup()
{
Serial.begin(9600);
Serial.println("*** START ***");
}
void loop()
{
profile[0] = (record_type) {(1,2,3),(5,7,9),(-40,-30,-25),(2,4,6)};
Serial.println(profile[0].setpt[2]); // fails with setpt[from 0 to 3]
}
Solution 1:[1]
Comma operators are used in your expressions like (1,2,3).
(1,2,3) is equivalent to 3 here.
You should use {} instead of () to initialize each arrays.
profile[0] = (record_type) {{1,2,3},{5,7,9},{-40,-30,-25},{2,4,6}};
Then, correct the index to get -30 instead of -25.
Serial.println(profile[0].setpt[1]);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MikeCAT |
