Results 1 to 4 of 4
i've problem with my source code
Code:
#include<stdio.h>
main()
{
int j;
float t[100],dt,dx,*r;
dt=0.01;
dx=0.1;
t[0]=0;
{
t[j]=t[j-1]+dt;
*r=t[j]/(dx*dx);
printf("t[%d] =%.2f\n",j,t[j]);
printf("\n\nr[%d] =%.2f\n",j,*r);
j++;
}
while(*r<=0.5);
}...
- 03-17-2010 #1Just Joined!
- Join Date
- Mar 2010
- Posts
- 1
[help]segmentation fault
i've problem with my source code
Code:#include<stdio.h> main() { int j; float t[100],dt,dx,*r; dt=0.01; dx=0.1; t[0]=0; { t[j]=t[j-1]+dt; *r=t[j]/(dx*dx); printf("t[%d] =%.2f\n",j,t[j]); printf("\n\nr[%d] =%.2f\n",j,*r); j++; } while(*r<=0.5); }
- 03-17-2010 #2
You didn't allocate or assign any memory to r...
Make mine Arch Linux
- 03-17-2010 #3
The segfault is probably occurring because you didn't malloc r, like gerard said. However, you have other problems too.
First, you don't initialize j. j will probably default to 0, but you should assign 0 to it initially.
Also, I assume you're trying to use a do-while loop? You forgot the "do".DISTRO=Arch
Registered Linux User #388732
- 03-19-2010 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The pointer variable is not pointing to any valid memory (float) address. This is why you are crashing. Better declaration:
P.S. If this is a school exercise, please observe that the forums do not allow help with such - see the terms of use for details of what is allowed.Code:float t[100],dt,dx,*r = (float*)malloc(sizeof(float));
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote