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

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.

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

Popular posts from this blog

Warning, the Safe Path is not accessible vm3 - Joomla! Forum - community, help and support

uppercase letters in url - Joomla! Forum - community, help and support

Joomla! Update is not offering Joomla 3 - Joomla! Forum - community, help and support