0

I'm having a segmentation fault which I'm not being able to identify. After reading about other similar problems I thought it could be that I wasn't checking if (img == NULL) but even after adding it the problem stands.

[snip]

//variables

int counter = 0;   
char *filename = NULL;
FILE *img = NULL;

[snip]

sprintf(filename, "%03i.jpg", counter);
img = fopen(filename, "w");

[snip]

0

char *filename = NULL; creates a pointer to char but doesn't allocate any memory for it. Later you try to sprintf into that address space, but given that it still points at NULL, you segfault.

Perhaps instead of a char * you can use a char array that is long enough to hold the terminated string "000.jpg"

1
  • Thanks for the help. But the segmentation fault still persists. Tried char filename[512]; and some variations but still happens. – jlsandre Jan 28 '20 at 22:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .