C++ strings and streams are very much a good idea, but if you absolutely can't use them, the following code might be useful. The first function writes the two doubles into an existing C string. The second function reads two doubles out of an existing C string and returns them by pointer:
void CoordinatesToString(double lat, double long, char *buffer, size_t len) {
assert(buffer != NULL);
assert(len > 0);
snprintf(buffer, len, "%f, %f", lat, long);
buffer[len - 1] = 0; /* ensure null terminated */
}
int StringToCoordinates(const char *string, double *outLatitude, double *outLongitude) {
assert(string != NULL);
assert(outLatitude != NULL);
assert(outLongitude != NULL);
return (2 == sscanf(string, "%f, %f", outLatitude, outLongitude));
}
Usage:
char buffer[128];
CoordinatesToString(90.0, -33.0, buffer, 128);
double lat, lng;
if (StringToCoordinates(buffer, &lat, &lng)) {
printf("lat: %f long %f\n", lat, lng);
}
BUT. C++ strings are designed for this sort of use. They don't suffer from potential overflow problems inherent to char
arrays, and you don't have to manually manage their memory--they resize to fit their contents as needed. Is there a reason why you want to avoid std::string
when you say you're otherwise okay with a C++ solution?