I'm writing a TCP socket in C to send location data for a project I'm working on.
So far, everything works, but I'm struggling with this seemingly simply problem. I'm trying to build a JSON String that will be sent over the socket. I have a character array (representative of the String) json
defined as:
char json[1024];
With a method prototype:
const char* build_json(void);
And method body:
const char* build_json(void) {
strcpy(json, "{");
strcat(json, "\"latitude\":");
sprintf(json, "%0.5f", latitude);
strcat(json, "}");
return json;
}
I know that latitude
is defined correctly and should be a float
of approximately 5 decimal places.
But when I call build_json();
, 38.925034}
is the only thing that is returned. Why is this the case? It appears that the call to sprintf
is overwriting what's already been written in json
.
Thanks for your help!