My answer is similar to cdhowie's, but with a key difference. He's right - you shouldn't repurpose NaN to mean "unset". (In fact, setting some arbitrary number like -1234.56789 is safer than that, since it's unlikely anything will equal that, and more likely that a calculation will result in a NaN.) And apparantly, NAN isn't defined in all math.h files, though if it's in yours, that is the easiest way.
I recommend repurposoing NULL for your task.
You effectively want to turn your double into a Nullable type, so why not actually do that?
double* numberPointers[1024];
memset(numberPointers, NULL, 1024 * sizeof(double));
int i = 0;
while(1){
if (numberPointers[i] != NULL){
printf ("%f", numberPointers[i]);
i++;
}
else {
break;
}
}
The part of this that is difficult is changing your calculation. Instead of something like
void fillArray(double[] number) {
int i;
for (i = 0; i < 1024; ++i) {
number[i] = someCalculation(i);
}
}
you'd need something like
void fillArray(double*[] numberPointers) {
int i;
double result;
double* d;
for (i = 0; i < 1024; ++i) {
result = someCalculation(i);
d = (double*)malloc(sizeof(double));
*d = result;
numberPointers[i] = d;
}
}
And of course, when you're done, you'd need to free
all the pointers in numberPointers
, but that should be easy:
int i;
for (i = 0; i < 1024; ++i) {
if (numberPointers(i) != NULL) {
free(numberPointers(i));
}
}