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. Question about an BYTE value changing into pointer, then array

Question about an BYTE value changing into pointer, then array

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
8 Posts 6 Posters 0 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.
  • Y Offline
    Y Offline
    yeah1000
    wrote on last edited by
    #1

    Hi, i am in trouble with the following: //function declarations// void function1(); void function2(BYTE *temp); //functions// void function1(){ BYTE temp; function2(&temp); } void function2(BYTE *temp){ int i; for(i=0;i<10;i++){ temp[i] = 1; } } How come temp can be used as an array in function2 ? function2 requires a pointer to BYTE value not a BYTE array... TY

    C N D 3 Replies Last reply
    0
    • Y yeah1000

      Hi, i am in trouble with the following: //function declarations// void function1(); void function2(BYTE *temp); //functions// void function1(){ BYTE temp; function2(&temp); } void function2(BYTE *temp){ int i; for(i=0;i<10;i++){ temp[i] = 1; } } How come temp can be used as an array in function2 ? function2 requires a pointer to BYTE value not a BYTE array... TY

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      yeah1000 wrote:

      function2 requires a pointer to BYTE value not a BYTE array...

      in C/C++, there is no (syntactical) difference between a pointer to a single variable and a pointer to the start of an array.

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • Y yeah1000

        Hi, i am in trouble with the following: //function declarations// void function1(); void function2(BYTE *temp); //functions// void function1(){ BYTE temp; function2(&temp); } void function2(BYTE *temp){ int i; for(i=0;i<10;i++){ temp[i] = 1; } } How come temp can be used as an array in function2 ? function2 requires a pointer to BYTE value not a BYTE array... TY

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #3

        The problem here is function2(); it is declared to take a pointer to BYTE and then assumes this pointer points to the first element of a BYTE[10] array. It is unfortunate that it compiles at all, but it has to because in C the name of array will automatically decay to the type of pointer to its first element[^].

        utf8-cpp

        CPalliniC 1 Reply Last reply
        0
        • N Nemanja Trifunovic

          The problem here is function2(); it is declared to take a pointer to BYTE and then assumes this pointer points to the first element of a BYTE[10] array. It is unfortunate that it compiles at all, but it has to because in C the name of array will automatically decay to the type of pointer to its first element[^].

          utf8-cpp

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

          Nemanja Trifunovic wrote:

          It is unfortunate that it compiles at all

          I don't think so. :)

          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?

          N 1 Reply Last reply
          0
          • CPalliniC CPallini

            Nemanja Trifunovic wrote:

            It is unfortunate that it compiles at all

            I don't think so. :)

            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]

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            CPallini wrote:

            I don't think so

            Care to explain? [edit] Never mind - my remark would make sense if his second function was declared to have an array as parameter - somehow I thought it was :) [/edit]

            utf8-cpp

            modified on Tuesday, November 24, 2009 2:24 PM

            I 1 Reply Last reply
            0
            • N Nemanja Trifunovic

              CPallini wrote:

              I don't think so

              Care to explain? [edit] Never mind - my remark would make sense if his second function was declared to have an array as parameter - somehow I thought it was :) [/edit]

              utf8-cpp

              modified on Tuesday, November 24, 2009 2:24 PM

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #6

              Well, which is cleaner? 1)

              for (i = 0; i < 7; i++)
              DoSomething (ptr [i]);

              for (i = 0; i < 7; i++)
              DoSomething (*(ptr+i));

              for (i = 0; i < 7; i++)
              DoSomething (*ptr++)

              1 & 2 do the same thing. 3 messes up ptr too. Iain.

              I have now moved to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need contract work done, give me a job! http://cv.imcsoft.co.uk/[^]

              N 1 Reply Last reply
              0
              • I Iain Clarke Warrior Programmer

                Well, which is cleaner? 1)

                for (i = 0; i < 7; i++)
                DoSomething (ptr [i]);

                for (i = 0; i < 7; i++)
                DoSomething (*(ptr+i));

                for (i = 0; i < 7; i++)
                DoSomething (*ptr++)

                1 & 2 do the same thing. 3 messes up ptr too. Iain.

                I have now moved to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), or need contract work done, give me a job! http://cv.imcsoft.co.uk/[^]

                N Offline
                N Offline
                Nemanja Trifunovic
                wrote on last edited by
                #7

                That's fine, but I don't see what it has to do with my observation that array decay was an unfortunate decision by creators of C. If it wasn't for that, you could declare a function to accept either an array or a pointer, but it would be a compile error if you passed a pointer when an array is expected (look at OP). [edit] Now that *I* looked at OP carefully, I see that my remark does not make sense. Somehow, I thought his second function was declared to take an array, and it really takes a pointer. [/edit]

                utf8-cpp

                modified on Tuesday, November 24, 2009 2:25 PM

                1 Reply Last reply
                0
                • Y yeah1000

                  Hi, i am in trouble with the following: //function declarations// void function1(); void function2(BYTE *temp); //functions// void function1(){ BYTE temp; function2(&temp); } void function2(BYTE *temp){ int i; for(i=0;i<10;i++){ temp[i] = 1; } } How come temp can be used as an array in function2 ? function2 requires a pointer to BYTE value not a BYTE array... TY

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  See here.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  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