Subject: Re: snprintf() problem or I am stupid C programmer
To: Ciarcinski, Adam \(ISS Brussels\) <ACiarcinski@iss.net>
From: Andrew Brown <atatat@atatdot.net>
List: current-users
Date: 07/17/2001 15:39:24
>#include <stdio.h>
>
>int main()
>{
>char *test = "Test";
>
>printf("%s\n", test);
>snprintf(test, 5, "Passed");
>printf("%s\n", test);
>
>return 0;
>}
>
>If you compile and run it - crash!

the wasy this is written, test is allocated as a char pointer on the
stack that points to a static (read-only) string.  you can printf()
it, but not snprintf() into it (write into it).

>If you change 'char *test' to 'char test[]' - works!

test in this case is allocated as a char array of length 5 on the
stack which is writeable.

-- 
|-----< "CODE WARRIOR" >-----|
codewarrior@daemon.org             * "ah!  i see you have the internet
twofsonet@graffiti.com (Andrew Brown)                that goes *ping*!"
andrew@crossbar.com       * "information is power -- share the wealth."