Are all the values in a single string, like "0.100 0.840 0.030 ...", or do you have a bunch of separate strings, like "0.100", "0.840", "0.030", etc.? If they're in a single string, are they separated by whitespace (tabs, spaces, newlines) or by printing characters (comma, semicolon)? Do you know how many values you have ahead of time (i.e., how big your float array needs to be)?
To convert a string representing a single floating point value to double, use strtod()
as follows:
char valueText[] = "123.45";
char *unconverted;
double value;
value = strtod(valueText, &unconverted);
if (!isspace(*unconverted) && *unconverted!= 0)
{
/**
* Input string contains a character that's not valid
* in a floating point constant
*/
}
Read up on strtod()
for details. unconverted
will point to the first character in the string that wasn't converted by strtod()
; if it isn't whitespace or 0, then your string isn't properly formatted for a floating point value and should be rejected.
If all the values are in a single string, you're going to have to break the string into distinct tokens. The easy (if somewhat unsafe) way to do this is to use strtok()
:
char input[] = "1.2 2.3 3.4 4.5 5.6 6.7 7.8";
char *delim = " "; // input separated by spaces
char *token = NULL;
for (token = strtok(input, delim); token != NULL; token = strtok(NULL, delim))
{
char *unconverted;
double value = strtod(token, &unconverted);
if (!isspace(*unconverted) && *unconverted != 0)
{
/**
* Input string contains a character that's not valid
* in a floating point constant
*/
}
}
Read up on strtok()
for details.
If you don't know how many values you have up front, you'll need to do some memory management. You can dynamically allocate an array of float of some initial size using malloc()
or realloc()
, and then extend it periodically using realloc()
:
#define INITIAL_EXTENT 10
double *array = NULL;
size_t arraySize = 0;
size_t arrayIdx = 0;
char input[] = ...; // some long input string
char *delim = ...; // whatever the delimiter set is
char *token;
/**
* Create the float array at some initial size
*/
array = malloc(sizeof *array * INITIAL_EXTENT));
if (array)
{
arraySize = INITIAL_EXTENT;
}
/**
* Loop through your input string
*/
for (token = strtok(input, delim); token != NULL; token = strtok(NULL, delim))
{
double val;
char *unconverted;
if (arrayIdx == arraySize)
{
/**
* We've reached the end of the array, so we need to extend it.
* A popular approach is to double the array size instead of
* using a fixed extent; that way we minimize the number
* of calls to realloc(), which is relatively expensive.
*
* Use a temporary variable to receive the result; that way,
* if the realloc operation fails, we don't lose our
* original pointer.
*/
double *tmp = realloc(array, sizeof *array * (arraySize * 2));
if (tmp != NULL)
{
array = tmp;
arraySize *= 2;
}
else
{
/**
* Memory allocation failed; for this example, we just exit the loop
*/
fprintf(stderr, "Memory allocation failed; exiting loop\n");
break;
}
}
/**
* Convert the next token to a float value
*/
val = strtod(token, &unconverted);
if (!isspace(*unconverted) && *unconverted != 0)
{
/**
* Bad input string. Again, we just bail.
*/
fprintf(stderr, "\"%s\" is not a valid floating-point number\n", token);
break;
}
else
{
array[arrayIdx++] = val;
}
}
Don't forget to free()
array when you're done with it.