I used malloc()
to make an array of floating point numbers like so:
float*x1;
x1 = (float*)malloc((vertexes/3)*sizeof(float));
if(x1 == NULL)
{
printf("Out Of Memory");
getchar(); return(1);
}
So far, it seems like the above is fine based on my limited knowledge, however when I attempt to use that array like this: fscanf(stl,"%f",x1[N]);
it does not work. The N in brackets after the x1 is a variable that normally gets incremented, but for testing purposes I quoted out all that and just used any number that was within the range of the array like 3 for example. When I try to do this, the program compiles and runs fine until it hits the fscanf
line of code. at that point it crashes and windows says its trying to find a solution to the problem. I tried using my dynamic array by putting x1[3] = 12345
which seemed to work, because printf("%f",x1[3]);
outputted 12345
like it was supposed to. This leads me to believe the problem lies within fscanf(stl,"%f",x1[N]);
but I have no clue why.
Thanks in advance for any advice, it is greatly appreaciated.
With the scanf
family, you need to provide the address of the variable you want populated, such as:
fscanf (stl, "%f", &(x1[N]));
From the C11
standard 7.20.6.2 The fscanf function / 12
(my emphasis):
a,e,f,g:
Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected for the subject sequence of the strtod function. The corresponding argument shall be a pointer to floating.
And, just a couple of other points:
malloc
in C. It can hide certain subtle errors, and C will happily implicitly cast without it. scanf
family since it gives you the number of items successfully scanned. If that's not what you asked for, you should take appropriate action.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.