Amit Gupta
Amit Gupta

Reputation: 287

program crashes during runtime while giving input

#include<stdio.h>

int main() {

  int cases,no,**event,i,j;
  scanf("%d",&cases);

  for(;cases>0;cases--) {
     scanf("%d",&no);
     event=(int **)malloc(no*sizeof(int *));
     for(i=0;i<no;i++) {
        event[i]=(int *) malloc(3*sizeof(int));
        for(j=0;j<3;j++) {
              scanf("%d",event[i][j]);
        }             
     }             
  }    

}

I tried debugging it with gdb. It states segmentation fault while giving input for the second row.

Upvotes: 2

Views: 90

Answers (1)

Musa
Musa

Reputation: 97672

Pass the address of event[i][j] to scanf

scanf("%d", &event[i][j]);

Upvotes: 2

Related Questions