- #1
Fb.Researcher
- 9
- 0
I have written a C program that produce a structure, give it's elements their initial values and change these values when neccessary.I can compile my program but when I try to run it,error:"segmentation fault(core dumped)" appears.I commented different parts of my program and I underentood the problem is related to my structur.The code is:
In my program there are many players,I give to 5000 of them a number netween 0 to 10000
which should not be repeated.I check if their number is repeated in a "while-loop".The "logic" varible helps me here.If it's value is "R" then the number is not repeated.If it's vlue is "w" then I should chose another number.The array "r" saves the random numbers chsen before.
Now I think the error is related to the line with printf,I put a /****/ in that line,because when I comment this line thre is no error anymore.
What do you thik yhe problem is?
How do you think we can solve this problem?
Code:
#define PN 5000 //number of players I chose
#define RN 10 //number of array's rows
#define N number of array's columns
enum logical {W,R};
main()
{
int j=0,i=0,logic=R,r[100000]={0};
struct plrrecord
{
int n;//identity number of the player
short int lfstat;//life status: dead or alive
short int ch[RN][N];
};
struct plrrecord plr[PN];
while( i<PN )
{
r[i]=(int)(drand48() * 10000);
for( j=0 ; j<i ; j++)
{ if( r[j]==r[i] )logic=W;}
if( logic==R )
{
cplr[r[i]].n=r[i];
i++;
}
else { logic = R; }
}
for(i=0;i<PN;i++)
printf("%i\t",cplr[i].n);/****/
return 0;
}
which should not be repeated.I check if their number is repeated in a "while-loop".The "logic" varible helps me here.If it's value is "R" then the number is not repeated.If it's vlue is "w" then I should chose another number.The array "r" saves the random numbers chsen before.
Now I think the error is related to the line with printf,I put a /****/ in that line,because when I comment this line thre is no error anymore.
What do you thik yhe problem is?
How do you think we can solve this problem?