Dynamic change of variable name
-
@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?
-
@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(); } -
@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.
-
@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...
-
@sergio-rius said in Dynamic change of variable name:
Notice the <=
That will cause a buffer overrun at the end again...
-
@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 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? ???
-
@sergio-rius said in Dynamic change of variable name:
Notice the <=
That will cause a buffer overrun at the end again...
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.
-
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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login