I am trying to implement a sketch using a esp8266 como servidor
, I need to send a string to the client and for that I am using this piece of code:
char sbuf[] = "Hello world!\n\r";
size_t len = strlen(sbuf);
for (i = 0; i < MAXCLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write((char*) &sbuf, len);
delay(1);
}
}
Everything funciona OK
and the client can receive the characters, now I want to do una funcion
this so I can call it when I need it, so I have tried to implement my function:
void sendDataToClient( char *sbuf[]) {
size_t len = strlen(sbuf) + 1;
for (i = 0; i < MAXCLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write((char*) &sbuf, len);
delay(1);
}
}
}
and so I am llamando a la funcion
:
char bufferMSGtoClient[] = "Hello world!\n\r\0";
sendDataToClient(bufferMSGtoClient);
But this no funcion
, the client does not receive anything, could someone tell me what myerror?
When you declare a function with an array as a parameter, the function actually receives a pointer to the first element of the array. In this case:
It is equivalent to:
Then getting the size of
sbuf
con givessizeof
you 4 (the size of achar *
on your system) and not the size of the array.You can fix this by explicitly passing the size when calling the function, for example:
If you don't want to pass the size as the second argument, you have two options:
strlen
to calculate the size dynamically:There is another error in this part:
sbuf
inside the function is a pointer to char (char *
) so you can pass it directly without casting it, so it looks like this:the way you use doesn't work because you're taking the address in memory from with the &
sbuf
operator , but it's already an address in memory (a pointer) so the result you get from is a (pointer to pointer to char).sbuf
&sbuf
char **