Help With Arrays
-
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!
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 -
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!
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
-
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 -
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).
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 -
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
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?
-
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).
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:
-
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?
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
-
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
-
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!
-
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