Ok so i want to understand what this does:
#define NR_END 1
float *vector(long nl, long nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
float *v;
v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
if (!v) nrerror("allocation failure in vector()");
return v-nl+NR_END;
}
i mean it creates a pointer to a malloc space of the size given there. so v[0] is the first unit ... and so on till the end of the block.
but why is some term returned in the end.
I thought you couldn't move anywhere on the stack with a pointer but just be able to access the space given?
int main()
{
printf("Hello world\n\n");
float* f = vector(3,5);
f[0]=1;
f[1]=2;
f[2]=3;
f[7] = 5;
printf("%f", f[7]);
return 0;
}
i did these tests and it compiles whithout errors or warnings when gcc filename.c -o filename.
and why does v[7]=5 work when it shouldn't?
and what is a subscript range?
and how are you supposed to use this correctly?
i am very confused. Please help me understand this.
the file seems to be also viewable at https://www.cfa.harvard.edu/~sasselov/rec/code/nrutil.c