Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • 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
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Access array data

Access array data

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
10 Posts 4 Posters 1 Views 1 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.
  • N Offline
    N Offline
    necy36
    wrote on last edited by
    #1

    A thumbwheel switch (swt) has been assigned a 16 element array to determine which number is selected. How would I access that data? When I try *swt, I recieved error messages and warnings. When I use swt[0-16], I get zeroes as the switch values.

    L CPalliniC _ 3 Replies Last reply
    0
    • N necy36

      A thumbwheel switch (swt) has been assigned a 16 element array to determine which number is selected. How would I access that data? When I try *swt, I recieved error messages and warnings. When I use swt[0-16], I get zeroes as the switch values.

      L Offline
      L Offline
      Larry J Siddens
      wrote on last edited by
      #2

      Without actually seeing your code and if you are using

      swt[0-16]

      Then the actual index into the array is -16.

      Larry J. Siddens

      N 1 Reply Last reply
      0
      • N necy36

        A thumbwheel switch (swt) has been assigned a 16 element array to determine which number is selected. How would I access that data? When I try *swt, I recieved error messages and warnings. When I use swt[0-16], I get zeroes as the switch values.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        What about a good C language tutorial? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • N necy36

          A thumbwheel switch (swt) has been assigned a 16 element array to determine which number is selected. How would I access that data? When I try *swt, I recieved error messages and warnings. When I use swt[0-16], I get zeroes as the switch values.

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          You can use only integral expressions as the switch expression. What type of array is it? If it is a character array, you must use the string functions in the if statement. If it is an integral array, you are storing multiple values. You must use the array subscript [] to specify which element to check.

          «_Superman_» I love work. It gives me something to do between weekends.

          N 1 Reply Last reply
          0
          • L Larry J Siddens

            Without actually seeing your code and if you are using

            swt[0-16]

            Then the actual index into the array is -16.

            Larry J. Siddens

            N Offline
            N Offline
            necy36
            wrote on last edited by
            #5

            Original code looks like this: if (local->decode) { swt[0] = 0; swt[1] = 1; swt[2] = 1; swt[3] = 1; up to [15] } locals->temp = 0; for (mode_index = 0; mode_index < 16; mode_index++) { if (swt[mode_index] == 1) { locals->temp += (1 << mode_index); } } ==================================== What I am attempting to do looks like this where I tried *swt and swt[0], swt[1], swt[2], etc...: nom3as = nim3as if (nom3as == 0) nom3ac = 0; else nom3as |= 0x00002000; nom3ac = swt;

            L 1 Reply Last reply
            0
            • _ _Superman_

              You can use only integral expressions as the switch expression. What type of array is it? If it is a character array, you must use the string functions in the if statement. If it is an integral array, you are storing multiple values. You must use the array subscript [] to specify which element to check.

              «_Superman_» I love work. It gives me something to do between weekends.

              N Offline
              N Offline
              necy36
              wrote on last edited by
              #6

              I tried using []. Will I have to use it for each element of the array?

              L _ 2 Replies Last reply
              0
              • N necy36

                I tried using []. Will I have to use it for each element of the array?

                L Offline
                L Offline
                Larry J Siddens
                wrote on last edited by
                #7

                Yes. Say you have an array: int p[] = { 5, 4, 3, 2, 1 }; then to get the 3rd element use: int x = p[2]; This will set x to 3. Remember, arrays start at index 0 instead of 1. To loop through for( int 1 = 0; i < 5; i++ ) { x = p[i]; }

                Larry J. Siddens

                1 Reply Last reply
                0
                • N necy36

                  I tried using []. Will I have to use it for each element of the array?

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #8

                  Yes. You will have 16 array elements ranging from swt[0] to swt[15]

                  «_Superman_» I love work. It gives me something to do between weekends.

                  1 Reply Last reply
                  0
                  • N necy36

                    Original code looks like this: if (local->decode) { swt[0] = 0; swt[1] = 1; swt[2] = 1; swt[3] = 1; up to [15] } locals->temp = 0; for (mode_index = 0; mode_index < 16; mode_index++) { if (swt[mode_index] == 1) { locals->temp += (1 << mode_index); } } ==================================== What I am attempting to do looks like this where I tried *swt and swt[0], swt[1], swt[2], etc...: nom3as = nim3as if (nom3as == 0) nom3ac = 0; else nom3as |= 0x00002000; nom3ac = swt;

                    L Offline
                    L Offline
                    Larry J Siddens
                    wrote on last edited by
                    #9

                    In your last line: nom3ac = swt; You're setting the variable nom3ac to the address of the start of the array (or the address of swt[0].) Is this correct?

                    Larry J. Siddens

                    N 1 Reply Last reply
                    0
                    • L Larry J Siddens

                      In your last line: nom3ac = swt; You're setting the variable nom3ac to the address of the start of the array (or the address of swt[0].) Is this correct?

                      Larry J. Siddens

                      N Offline
                      N Offline
                      necy36
                      wrote on last edited by
                      #10

                      Yes, that's correct.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups