A little problem in C and causing core dumped

Hello! I’ve just started coding and I encounter this problem. My question is that when I use this code, I get a segmentation fault (core dumped). However, when I swap size and quantity my code, it runs successfully. It would be a pleasure if anyone could clarify me what causes this error.

It’s hard to say much here without full context, but “segmentation fault” means that your process accessed memory it doesn’t own. So there’s a pointer problem somewhere.

One thing that doesn’t explain the segmentation fault but it is probably part of the problem: The value returned by fread() is not the number of bytes, but the number of items (quantity) that have been read. From the manual page:

Upon successful completion, fread() shall return the number of elements successfully read which is less than nitems only if a read error or end-of-file is encountered.

1 Like