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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help With Arrays

Help With Arrays

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelp
14 Posts 5 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.
  • R Russell

    simoncoul wrote:

    return *sendbuf2;

    and

    simoncoul wrote:

    unsigned char myclase::makesendbuffer(Packet SendPacket)

    :doh: the functions needs to return an unsigned char, not a pointer (to an array) ...something could be wrong... I think that you want to pass all the array...then: unsigned char***** myclase::makesendbuffer(Packet SendPacket) and then something like return sendbuf2;:~ But you can't return a pointer to a local variable if you want that everything runs well.... :(( The solution could be pass to the function a pointer to a vector and fill it inside the function :) -- modified at 14:14 Wednesday 8th August, 2007


    Russell

    S Offline
    S Offline
    simoncoul
    wrote on last edited by
    #3

    Thanks for the help I got it to work

    unsigned char sendbuf[6];
    myclass::makesendbuffer(SendPacket, sendbuf);

    unsigned char myclase::makesendbuffer(Packet SendPacket, unsigned char sendbuf2[6]){
    unsigned char sendbuf2[6];

    if(SendPacket.data < 0){
    	SendPacket.data += 65536;
    }
    sendbuf2\[0\] = SendPacket.data % 256;
    sendbuf2\[1\] = floor(double(SendPacket.data/256));	
    sendbuf2\[2\] = SendPacket.address % 256;
    sendbuf2\[3\] = floor(double(SendPacket.address/256));
    sendbuf2\[4\] = SendPacket.command;
    
    return sendbuf2;
    

    }

    Made much for sense to send a pointer to the array and do stuff to in in the function then what ever I was trying to do!

    J D R 3 Replies Last reply
    0
    • S simoncoul

      Hi I'm trying to write a function that will return an array this is what I have

      unsigned char sendbuf[6];
      sendbuf[0] = myclass::makesendbuffer(SendPacket);

      unsigned char myclase::makesendbuffer(Packet SendPacket){
      unsigned char sendbuf2[6];

      if(SendPacket.data < 0){
      	SendPacket.data += 65536;
      }
      sendbuf2\[0\] = SendPacket.data % 256;
      sendbuf2\[1\] = floor(double(SendPacket.data/256));	
      sendbuf2\[2\] = SendPacket.address % 256;
      sendbuf2\[3\] = floor(double(SendPacket.address/256));
      sendbuf2\[4\] = SendPacket.command;
      
      return \*sendbuf2;
      

      }

      The return is just giving me the value of the first element and everything else is not used. Any ideas of what I'm doing wrong here would be great thanks! Simon

      J Offline
      J Offline
      jhwurmbach
      wrote on last edited by
      #4

      simoncoul wrote:

      Any ideas of what I'm doing wrong here would be great thanks!

      You are programming in C. This might or might not be intended. The C++-way would be a std::vector. BTW: You can easily hand that over to C-Functions by getting the address of the first element: &vec[0]. I assume that you want to use C(ish) Code. I see two errors: 1) You are allocating the sendbuf in your function on the stack. The memory will be freed on leaving the function. Later access to it will fail. When you don't expect it. Catastrophically. You either need to allocate it outside and hand a pointer in, or you need to allocate it in the function on the heap and return the pointer you got (And delete it after use!). 2) Your code explicitly states that it is returning exactly one unsigned char. What you want is returning the array of chars, I think. So you need to return a pointer to the first element of the array and the length. Alternatively, when the length is always 6, you could define a datatype of 6 unsigned chars to be a sendbuf, and return only the sendbuf-pointer.


      Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
      George Orwell, "Keep the Aspidistra Flying", Opening words

      1 Reply Last reply
      0
      • S simoncoul

        Thanks for the help I got it to work

        unsigned char sendbuf[6];
        myclass::makesendbuffer(SendPacket, sendbuf);

        unsigned char myclase::makesendbuffer(Packet SendPacket, unsigned char sendbuf2[6]){
        unsigned char sendbuf2[6];

        if(SendPacket.data < 0){
        	SendPacket.data += 65536;
        }
        sendbuf2\[0\] = SendPacket.data % 256;
        sendbuf2\[1\] = floor(double(SendPacket.data/256));	
        sendbuf2\[2\] = SendPacket.address % 256;
        sendbuf2\[3\] = floor(double(SendPacket.address/256));
        sendbuf2\[4\] = SendPacket.command;
        
        return sendbuf2;
        

        }

        Made much for sense to send a pointer to the array and do stuff to in in the function then what ever I was trying to do!

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #5

        And this very code does work as expected? :wtf:


        Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
        George Orwell, "Keep the Aspidistra Flying", Opening words

        S 1 Reply Last reply
        0
        • S simoncoul

          Thanks for the help I got it to work

          unsigned char sendbuf[6];
          myclass::makesendbuffer(SendPacket, sendbuf);

          unsigned char myclase::makesendbuffer(Packet SendPacket, unsigned char sendbuf2[6]){
          unsigned char sendbuf2[6];

          if(SendPacket.data < 0){
          	SendPacket.data += 65536;
          }
          sendbuf2\[0\] = SendPacket.data % 256;
          sendbuf2\[1\] = floor(double(SendPacket.data/256));	
          sendbuf2\[2\] = SendPacket.address % 256;
          sendbuf2\[3\] = floor(double(SendPacket.address/256));
          sendbuf2\[4\] = SendPacket.command;
          
          return sendbuf2;
          

          }

          Made much for sense to send a pointer to the array and do stuff to in in the function then what ever I was trying to do!

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

          simoncoul wrote:

          unsigned char myclase::makesendbuffer(Packet SendPacket, unsigned char sendbuf2[6]) { unsigned char sendbuf2[6];

          There's no way you could have gotten this to compile.


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          S 1 Reply Last reply
          0
          • J jhwurmbach

            And this very code does work as expected? :wtf:


            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
            George Orwell, "Keep the Aspidistra Flying", Opening words

            S Offline
            S Offline
            simoncoul
            wrote on last edited by
            #7

            Yeah that code works perfectly, since I'm sending the point of sendbuf, I am able to manipulate it as if it was being declared inside of the function. I dunno if this is the correct C++ way of doing it but it makes sense to me(but I know know C).

            J M 2 Replies Last reply
            0
            • S simoncoul

              Yeah that code works perfectly, since I'm sending the point of sendbuf, I am able to manipulate it as if it was being declared inside of the function. I dunno if this is the correct C++ way of doing it but it makes sense to me(but I know know C).

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #8

              I just asked, because your format of the second parameter is, well, uncommon. And you are using the identifier sendbuf2 twice.


              Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
              George Orwell, "Keep the Aspidistra Flying", Opening words

              1 Reply Last reply
              0
              • D David Crow

                simoncoul wrote:

                unsigned char myclase::makesendbuffer(Packet SendPacket, unsigned char sendbuf2[6]) { unsigned char sendbuf2[6];

                There's no way you could have gotten this to compile.


                "A good athlete is the result of a good and worthy opponent." - David Crow

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                S Offline
                S Offline
                simoncoul
                wrote on last edited by
                #9

                int main(void){
                unsigned char sendbuf[6];
                myclass::makesendbuffer(SendPacket, sendbuf);
                }

                void myclass::makesendbuffer(Packet SendPacket, unsigned char sendbuf[6]){
                if(SendPacket.data < 0){
                SendPacket.data += 65536;
                }
                sendbuf[0] = SendPacket.data % 256;
                sendbuf[1] = floor(double(SendPacket.data/256));
                sendbuf[2] = SendPacket.address % 256;
                sendbuf[3] = floor(double(SendPacket.address/256));
                sendbuf[4] = SendPacket.command;
                }

                That was what I finally ended up with and it works, is there a problem with it that u can see?

                D 1 Reply Last reply
                0
                • S simoncoul

                  Yeah that code works perfectly, since I'm sending the point of sendbuf, I am able to manipulate it as if it was being declared inside of the function. I dunno if this is the correct C++ way of doing it but it makes sense to me(but I know know C).

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #10

                  simoncoul wrote:

                  I dunno if this is the correct C++ way of doing it but it makes sense to me

                  hmmm... Maybe take another look :) Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  • S simoncoul

                    int main(void){
                    unsigned char sendbuf[6];
                    myclass::makesendbuffer(SendPacket, sendbuf);
                    }

                    void myclass::makesendbuffer(Packet SendPacket, unsigned char sendbuf[6]){
                    if(SendPacket.data < 0){
                    SendPacket.data += 65536;
                    }
                    sendbuf[0] = SendPacket.data % 256;
                    sendbuf[1] = floor(double(SendPacket.data/256));
                    sendbuf[2] = SendPacket.address % 256;
                    sendbuf[3] = floor(double(SendPacket.address/256));
                    sendbuf[4] = SendPacket.command;
                    }

                    That was what I finally ended up with and it works, is there a problem with it that u can see?

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

                    simoncoul wrote:

                    is there a problem with it that u can see?

                    No, assuming all of the values being assigned to sendbuf are between 0 and 255.


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    S 1 Reply Last reply
                    0
                    • D David Crow

                      simoncoul wrote:

                      is there a problem with it that u can see?

                      No, assuming all of the values being assigned to sendbuf are between 0 and 255.


                      "A good athlete is the result of a good and worthy opponent." - David Crow

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      S Offline
                      S Offline
                      simoncoul
                      wrote on last edited by
                      #12

                      Yeah they are! Thanks for all the help guys!!

                      1 Reply Last reply
                      0
                      • S simoncoul

                        Thanks for the help I got it to work

                        unsigned char sendbuf[6];
                        myclass::makesendbuffer(SendPacket, sendbuf);

                        unsigned char myclase::makesendbuffer(Packet SendPacket, unsigned char sendbuf2[6]){
                        unsigned char sendbuf2[6];

                        if(SendPacket.data < 0){
                        	SendPacket.data += 65536;
                        }
                        sendbuf2\[0\] = SendPacket.data % 256;
                        sendbuf2\[1\] = floor(double(SendPacket.data/256));	
                        sendbuf2\[2\] = SendPacket.address % 256;
                        sendbuf2\[3\] = floor(double(SendPacket.address/256));
                        sendbuf2\[4\] = SendPacket.command;
                        
                        return sendbuf2;
                        

                        }

                        Made much for sense to send a pointer to the array and do stuff to in in the function then what ever I was trying to do!

                        R Offline
                        R Offline
                        Russell
                        wrote on last edited by
                        #13

                        you are wellcome... but

                        simoncoul wrote:

                        unsigned char sendbuf2[6];

                        cut this line and I think that is better if **void** myclase::makesendbuffer(Packet SendPacket, unsigned char***** sendbuf2) ;)


                        Russell

                        S 1 Reply Last reply
                        0
                        • R Russell

                          you are wellcome... but

                          simoncoul wrote:

                          unsigned char sendbuf2[6];

                          cut this line and I think that is better if **void** myclase::makesendbuffer(Packet SendPacket, unsigned char***** sendbuf2) ;)


                          Russell

                          S Offline
                          S Offline
                          simoncoul
                          wrote on last edited by
                          #14

                          ok Thanks I see what u mean doesn't make sense to say sendbuf[6]. Thanks again!

                          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