0

I am trying to get random numbers between 0 and 11 but I am getting segmentation fault core dumped error here is my code:

void track_machine(){
        int *X = 0,*Y = 0; //Coordinates
        double *D = 0,*R = 0; //Distance and replacement
        refresh_position(X,Y,D,R);
        printf("%d",*X);
        printf("%d",*Y);
}
void refresh_position(int *X, int *Y, double *D, double *R){
        *X = rand();
        *Y = rand();
}

I am running the track_machine() from my main function then i get an error.

Baran
  • 131
  • 8
  • 1
    You passing `NULL` (`0`) pointers and then derreferencing it (`*X = rand();`) – Eraklon Mar 26 '20 at 12:45
  • 1
    You need to declare actual `int` variables (i.e. provide storage space for them), and then pass their addresses to `refresh_position`. So, use `int X,Y,D,R;` and then `refresh_position(&X,&Y,&D,&R);`. Consider placing there values inside meaningful structs and passing pointers to these structs around. – vgru Mar 26 '20 at 12:52
  • And most importantly, you need to read the chapter dealing with pointers in your C text book – Jabberwocky Mar 26 '20 at 13:55
  • Thanks for your comments i changed the code as you say and study little bit of pointers. Now i solved my problem. – Baran Mar 26 '20 at 14:06

0 Answers0