Hello all
I’ve spent a day trying to figure this out. I cannot seem to get fwrite to work. Everything else seems to run perfectly. Here’s what happens:
int a = 0;
printf("%d", a);
Prints 0, as expected
int a = 0;
fwrite(&a, sizeof(int), 1, stdout);
Prints 4 bytes of random garbage.
But:
int a = 0;
int b;
memcpy(&b, &a, sizeof(int));
printf("%d", b);
Prints 0 again, as expected. So pointers work. Same with malloc(), seems to work. But fwrite always fails. Why?
And finally,
int a = 0;
char* b = (char*)&a;
fputc(b[0], stdout);
fputc(b[1], stdout);
fputc(b[2], stdout);
fputc(b[3], stdout);
fflush(stdout);
Also prints garbage. So I guess the problem is in fputc? Or am I missing something obvious?
Edit: stupid me, the problem was a buggy serial terminal As soon as I tried something different, the output was correct. I am not using gtkterm anymore! :evil: