Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Dynamic change of variable name

Dynamic change of variable name

Scheduled Pinned Locked Moved Troubleshooting
14 Posts 7 Posters 1.5k Views 6 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Yveaux

    @greg_2502 do you mean to fill an array?
    Something like

    int value[5];
    for (i=1; i<5; i++){
      value[i] = debouncer[i].read();
    }
    
    skywatchS Offline
    skywatchS Offline
    skywatch
    wrote on last edited by
    #4

    @yveaux Shouldn't that be int value[4]; ? ;)

    mfalkviddM 1 Reply Last reply
    0
    • skywatchS skywatch

      @yveaux Shouldn't that be int value[4]; ? ;)

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #5

      @skywatch not if the 0th position is skipped

      skywatchS 1 Reply Last reply
      1
      • mfalkviddM mfalkvidd

        @skywatch not if the 0th position is skipped

        skywatchS Offline
        skywatchS Offline
        skywatch
        wrote on last edited by
        #6

        @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?

        mfalkviddM 1 Reply Last reply
        0
        • skywatchS skywatch

          @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?

          mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #7

          @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();
          }
          
          Sergio RiusS skywatchS 2 Replies Last reply
          0
          • mfalkviddM mfalkvidd

            @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();
            }
            
            Sergio RiusS Offline
            Sergio RiusS Offline
            Sergio Rius
            wrote on last edited by Sergio Rius
            #8

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

            Y 1 Reply Last reply
            0
            • Sergio RiusS Sergio Rius

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

              Y Offline
              Y Offline
              Yveaux
              Mod
              wrote on last edited by
              #9

              @sergio-rius said in Dynamic change of variable name:

              Notice the <=

              That will cause a buffer overrun at the end again...

              http://yveaux.blogspot.nl

              skywatchS Sergio RiusS 2 Replies Last reply
              2
              • Y Yveaux

                @sergio-rius said in Dynamic change of variable name:

                Notice the <=

                That will cause a buffer overrun at the end again...

                skywatchS Offline
                skywatchS Offline
                skywatch
                wrote on last edited by
                #10

                @yveaux Can you please explain why as I want to learn why it won't run the code the way I expected it to! -

                1 Reply Last reply
                0
                • mfalkviddM mfalkvidd

                  @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();
                  }
                  
                  skywatchS Offline
                  skywatchS Offline
                  skywatch
                  wrote on last edited by skywatch
                  #11

                  @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? ???

                  1 Reply Last reply
                  0
                  • Y Yveaux

                    @sergio-rius said in Dynamic change of variable name:

                    Notice the <=

                    That will cause a buffer overrun at the end again...

                    Sergio RiusS Offline
                    Sergio RiusS Offline
                    Sergio Rius
                    wrote on last edited by
                    #12

                    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.

                    B 1 Reply Last reply
                    0
                    • Sergio RiusS Sergio Rius

                      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.

                      B Offline
                      B Offline
                      badmannen
                      wrote on last edited by
                      #13

                      @sergio-rius
                      the loop will run even at "5".
                      that is the problem. if you write "i<5" , then it would be ok

                      rPi 3 - UNO R3 - Mini - Nano - custom

                      1 Reply Last reply
                      0
                      • Sergio RiusS Offline
                        Sergio RiusS Offline
                        Sergio Rius
                        wrote on last edited by
                        #14

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

                        1 Reply Last reply
                        0

                        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
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        11

                        Online

                        12.0k

                        Users

                        11.2k

                        Topics

                        113.4k

                        Posts


                        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • MySensors
                        • OpenHardware.io
                        • Categories
                        • Recent
                        • Tags
                        • Popular