Dynamic change of variable name
-
Hi, I can't find a solution to my problem.
I'd like to change the name in a function in a for loop. If it is possible?for (i=1; i<5; i++){ int value[i] = debouncer[i].read(); }
-
Not to my knowledge.
Generally you'd create a function that takes an input value, and change that?
-
@greg_2502 do you mean to fill an array?
Something likeint value[5]; for (i=1; i<5; i++){ value[i] = debouncer[i].read(); }
-
@yveaux Shouldn't that be int value[4]; ?
-
@skywatch not if the 0th position is skipped
-
@mfalkvidd Hehehe......
But i starts at 1 yes? and then increments in interger units whilst it is less than 5. So that should give 1,2,3,4. Or have I been under a misunderstanding?
-
@skywatch arrays indexes in c/c++ always start at 0.
int myarray[5] would have positions 0 thorugh 4. Position 5 would be outside the array. In the code above, position 0 is never used.So either make the array 1 element larger than actually needed (with position 0 unused), or let i start at 0:
int value[4]; for (i=0; i<4; i++){ value[i] = debouncer[i].read(); }
-
@mfalkvidd said in Dynamic change of variable name:
int myarray[5] would have positions 0 thorugh 4. Position 5 would be outside the array. In the code above, position 0 is never used.
So either make the array 1 element larger than actually needed (with position 0 unused), or let i start at 0:
In fact, I interpreted it more like @skywatch
Notice the <=int value[5]; for (i=1; i<=5; i++){ value[i] = debouncer[i].read(); }
I just made something like this for indexing sensors in a node, where idx 0 was the node itself, and thus already filled before the loop.
-
@sergio-rius said in Dynamic change of variable name:
Notice the <=
That will cause a buffer overrun at the end again...
-
@yveaux Can you please explain why as I want to learn why it won't run the code the way I expected it to! -
-
@mfalkvidd That won't get '0 through 4' as it will never produce i=4 in this case. I would expect it to need to be
int value[4]; for (i=0; i<=4; i++){ value[i] = debouncer[i].read(); }
in order to get 0 through 4 (note the extra '=' in line 2 above to make this work. Or am I still misunderstanding something? ???
-
It's as easy as trying it.
@yveaux said in Dynamic change of variable name:
That will cause a buffer overrun at the end again...
Really? I can't see why a loop trough 1, 2, 3, 4 could overflow a 5 elements array.
In my code, I manually add 0 (the device itself, for vcc) before entering the loop. So in this case, still 5 elements.
-
@sergio-rius
the loop will run even at "5".
that is the problem. if you write "i<5" , then it would be ok
-
@badmannen @Yveaux You're absolutely right. I was lazy, only changed a symbol and gave a bad advice. My response should be changed to <=4.
Still I don't understand what the original question was.
Suggested Topics
-
Day 1 - Status report
Announcements ā¢ 23 Mar 2014, 22:45 ā¢ hek 24 Mar 2014, 20:12 -
JSN-SR04T-V3.0 Coax cable extended
Troubleshooting ā¢ 12 days ago ā¢ bocalexandru 10 days ago -
Forum Search not working?
Troubleshooting ā¢ 4 Oct 2023, 23:33 ā¢ Gibber 2 Sept 2024, 20:28 -
Getting system time from the controller
Troubleshooting ā¢ 27 Feb 2025, 01:39 ā¢ dpcons 29 days ago -
Can not compile MySensors on esp8266
Troubleshooting ā¢ 24 Aug 2024, 15:35 ā¢ TheoL 29 Aug 2024, 20:47 -
Compiling Sensor code using BME280 and ESP8266
Troubleshooting ā¢ 26 Feb 2025, 00:32 ā¢ dpcons 26 Feb 2025, 06:22