# segmentation fault (core dumped) C error for structures

by Fb.Researcher
Tags: segmentation fault, structure
 P: 7 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:  #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
 Admin P: 22,708 Plenty of problems, I doubt what you posted compiles. Why do you use drand48() instead of a simple rand()? What is the maximum possible value of (int)(drand48()*10000)?
 P: 4,570 100% agree with Borek: there are just way too many problems with that code snippet.
P: 194

## segmentation fault (core dumped) C error for structures

I've forgotten more than I ever knew about C, but I do note that you define the player variable as 'plr' but then assign values to 'cplr'. Is that the kind of thing that might lead to pointers gleefully messing with memory?
 P: 7 The problem with calling plr instead of cplr was not serous,it happend when I was omitting extra rows of my program to make it simpler( I did it so that parts that are not related to the problem do not botter us) The code is: #include #include #define PN 10 //number of players I chose #define RN 10 //number of array's rows #define N 2 //number of array's columns enum logical {W,R}; main() {printf("Hi"); 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]; }; printf("environmental sourc:"); struct plrrecord cplr[PN]; while( i<10 ) { r[i]=(int)(drand48() * 10000); for( j=0 ; j
 Admin P: 22,708 What is the number of the last element in table[10000]? What is the maximum value of (int)(drand48() * 10000)?
 P: 7 Before changing the first program,a numer like r1 between 0 and 10000-1 was saved in the 9999th element of array so the array was chosen in a way so that always the program have an empty element for it. dran48() gives a random number beween 0 and 1(not including 1 or a number in scale [0 1) ) so drand48()*10000 gives a random number between 0 and 10000 and (int)(drand48() * 10000 ) ranges from 0 to 9999 (including 9999 or an integer number in scale [0 9999])

 Related Discussions Engineering, Comp Sci, & Technology Homework 1 Programming & Computer Science 4 Programming & Computer Science 8 Programming & Computer Science 9 Computing & Technology 5