variable size array
hello,
i trying make code send request , receive array of byte, problem don't know size of buffer receive
i know resolve problem creating array bigger maximum size think there clever solution maybe see
i tried pointers started them morning , m not use them
i hope see better solution mine
i trying make code send request , receive array of byte, problem don't know size of buffer receive
i know resolve problem creating array bigger maximum size think there clever solution maybe see
i tried pointers started them morning , m not use them
code: [select]
byte startcomunication[] = {2, 3, 6, 1, 78};
byte storehere[60];
void setup() {
serial.begin(9600);
serial.println("sending");
sendrequest(startcomunication, sizeof(startcomunication)); //sending know size array
serial.println("receiving");
receivedata(storehere); //receiving unknown size array
(int = 0; < 60; i++) {
serial.println(storehere[i]);
}
}
void loop() {}
void sendrequest(uint8_t *tosend, uint8_t sendlen) {
(byte = 0; < sendlen; i++) {
serial.println(tosend[i]);
}
}
void receivedata(byte *toreceive) {
byte buffersize = random (0,60); //random buffer received, how discover size?
(byte = 0; < buffersize; i++) {
toreceive[i] = random(0, 254); //values received
}
}
i hope see better solution mine
you can allocate buffer size.
*edit: mentioned buffer of chars, changed code reflect that
code: [select]
char buffersize = random (0,60);
char *buffer_p = (char*)malloc(sizeof(char)*buffersize);
// buffer_p can used array of chars of size buffersize, ie ...
buffer_p[0] = 4;
// must free after you've finished it, else memory persist, ie ...
free(buffer_p);
*edit: mentioned buffer of chars, changed code reflect that
Arduino Forum > Using Arduino > Programming Questions > variable size array
arduino
Comments
Post a Comment