Segmentation fault (core dumped) in c (sending text to array) -


i'm writing code takes input command line (a redirected text file) , sends data array in c. whenever run segmentation fault (core dumped) error. here code:

  int main(int argc, const char * argv[]) {     char *experiments[20];     int data[10][20];  int b=0; char *name = null; name=malloc(100); sendtoarray(data, experiments); while(b==0){     int input=0;     printf("\n");     printf("data set analysis\n");     printf("1. show data\n");     printf("2. calculate average experiment\n");     printf("3. calculate average across experiments\n");     printf("4. quit\n");     printf("selection: ");     scanf("%d", &input);      switch (input) {         case 1:             if(argc!=0){                 displayall(data, experiments);             }             else{                 printf("oops, went wrong!\n");             }             break;         case 2:             if(argc!=0){                 printf("\n");                 printf("enter name of experiment: ");                 scanf("%[^\n]", name);                 individualaverage(name, experiments, data);             }             else{                 printf("oops, went wrong!\n");             }             break;         case 3:             if(argc!=0){                 allaverage(experiments, data);             }             else{                 printf("oops, went wrong!\n");             }             break;         case 4:b=1;;             break;          default:             printf("oops, went wrong!\n");             break;     } } return 0; 

}

thats beginning of main, calls function:

void sendtoarray(int data[10][20], char *experiments[20]){ char line[100]; char line2[100];   char *temp1; int temp; int i=0, c=0; while(!strcmp(line, "*** end ***")){     scanf("%[^\n]", line);     scanf("%*c");     scanf("%[^\n]", line2);     scanf("%*c");     for(i=0; i<10; i++){          temp1 = strtok(line2, " ");         temp = atoi(temp1);         data[c][i] = temp;     }     experiments[c] = line;     c++;  } numexperiments = c+1;   } 

i changed function allocate 100 bits of memory each line, still core dump. also, if redirect text file, program loops through main infinitely. if can me find mistake!

here sample input file!, called in bash ./dataset < textfile :

experiment 1 3 10 8 7 3 2 9 7 5 6 experiment 2 10 20 30 40 50 60 70 80 90 100 control group 5 5 5 5 5 5 5 5 5 5 *** end *** 

okay, thank everyone, fixed memory dump, however, whenever pipe text file program still loops infinitely.

the problem never allocate memory line , line2. declare them proper arrays, e.g.:

char line[32]; char line2[32]; 

this causing segmentation fault, fixing have error: passing local addresses experiments array. can fix either allocating line arrays via malloc, or allocating proper memory experiments , copying each string in line strcpy or something. also, pretty sure that

scanf("%[^\n]\n", line); 

is invalid. want is

scanf("%[^\n]", line); scanf("%*c"); 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -