/*
Is this a bug in gcc or djgpp or did i only drank to much coffe?
Description: Tries to copy the string 'test' into the
variable 'testvar', sprintf() returns that 4 bytes was
successfully written, but when i trys to display the
string it says it's empty.
Output from program below:
--------------------------
Wrote 4 bytes
String now is (null)
Compiled with gcc foo.c -o foo.exe -Wall
*/
#include <stdio.h>
#include <string.h>
int main()
{
unsigned char *testvar;
printf("Wrote %d bytes\n", sprintf(testvar,"test") );
printf("String now is %s\n",testvar);
return 0;
}
The bug is in the example code, not in DJGPP: 'testvar' is not initialized
to any writable storage. Doing anything through a wild pointer (including
trying to sprintf() to it) causes undefined behaviour, by definition of
the C standard. On my Linux box here, e.g., the example program gave
a segfault, instead.
BTW: test compilations should be done with '-Wall -O', not just '-Wall'.
See the description of '-Wuninitialized' in 'info gcc', for the reason.
Adding '-W' and/or '-ansi -pedantic' to the command line may also be
useful.