pointers & arrays and realloc!
Hi..I need some help in using the realloc function in my program as the memory i need to use keeps on changing based on some parameters.Do i first have to use the malloc function and then realloc?
I need to know how to use malloc, but in my programme I am using arrays for example t and x both of which are arrays..When i try to declare them as t[],x[] as i dont know how big the arrays should be it doesnt work, so how do i declare arrays when i dont know the size they should be, so that i can use the malloc function.I know that an array is a pointer to its first value, but i dnt know how to implement this..basically i need help!
this is a bit of my programme.Only the arrays time and x(t) are output to my external file, so would i have to use malloc on all arrays used in my program or only those that are output?
Code:
float m, k, g, x[], v[], time_increment,time[], w2, w, p, f[], A, omega;
fprintf(output, "%f %f\n", g, w);
time[0]=0;
i=-1;
while((time[i]>=0)&&(time[i] < p))
{
if(CASE_FLAG==1)
{
f[i]=0;
}
else if(CASE_FLAG==2)
{
f[i]=A*cos(omega*time[i]);
}
i++;
time[i] = i*time_increment;
v[i+1] = v[i] - (g*v[i] + w2*x[i])*time_increment + (1/m)*f[i]*time_increment;
x[i+1] = x[i] + v[i+1]*time_increment;
fprintf(output, "%f %f\n", time[i], x[i]);
}
fclose(output);
many thanx
-zesty-