c++.dos - sprintf with char
- noobi (12/12) Aug 01 2003 hello,
- Walter (4/16) Aug 01 2003 Because achar is statically initialized to NULL - your program never set...
- noobi (4/25) Aug 02 2003 I need to put the Ascii character of code variable in a buffer (achar)
- Walter (7/37) Aug 02 2003 it
- Gisle Vanem (10/16) Aug 01 2003 I'm amased this doesn't crash (assuming it's a 32-bit program).
- Walter (3/16) Aug 02 2003 No, it doesn't. Sorry.
- raju (15/15) Oct 31 2008 Hello,
hello, #include <stdio.h> char *achar; int code; main() { code = 65; sprintf(achar, "%c", code); printf("char1=%c \n",code); // output > char1=A printf("char2=%s",achar); // output > char2=(null) } Why char2=(null) and not A ?
Aug 01 2003
"noobi" <noobi_member pathlink.com> wrote in message news:bgdrgp$f8i$1 digitaldaemon.com...hello, #include <stdio.h> char *achar; int code; main() { code = 65; sprintf(achar, "%c", code); printf("char1=%c \n",code); // output > char1=A printf("char2=%s",achar); // output > char2=(null) } Why char2=(null) and not A ?Because achar is statically initialized to NULL - your program never sets it to point to a buffer.
Aug 01 2003
In article <bge9fl$svp$1 digitaldaemon.com>, Walter says..."noobi" <noobi_member pathlink.com> wrote in message news:bgdrgp$f8i$1 digitaldaemon.com...I need to put the Ascii character of code variable in a buffer (achar) and display it with printf() later How can i do it ?hello, #include <stdio.h> char *achar; int code; main() { code = 65; sprintf(achar, "%c", code); printf("char1=%c \n",code); // output > char1=A printf("char2=%s",achar); // output > char2=(null) } Why char2=(null) and not A ?Because achar is statically initialized to NULL - your program never sets it to point to a buffer.
Aug 02 2003
"noobi" <noobi_member pathlink.com> wrote in message news:bgfpf7$2bh6$1 digitaldaemon.com...In article <bge9fl$svp$1 digitaldaemon.com>, Walter says...it"noobi" <noobi_member pathlink.com> wrote in message news:bgdrgp$f8i$1 digitaldaemon.com...hello, #include <stdio.h> char *achar; int code; main() { code = 65; sprintf(achar, "%c", code); printf("char1=%c \n",code); // output > char1=A printf("char2=%s",achar); // output > char2=(null) } Why char2=(null) and not A ?Because achar is statically initialized to NULL - your program never setsWrite: char achar[2]; achar[0] = code; achar[1] = 0;to point to a buffer.I need to put the Ascii character of code variable in a buffer (achar) and display it with printf() later How can i do it ?
Aug 02 2003
"noobi" <noobi_member pathlink.com> wrote:char *achar; int code; main() { code = 65; sprintf(achar, "%c", code);I'm amased this doesn't crash (assuming it's a 32-bit program). If it's is a 16-bit program, it shouldn't crash, but give a "NULL pointer assignment" message at program exit. Ref. Borland's c0.asm. Walter, does DMC have a similar __checknull() function? -- Gisle V. /bin/laden: Not found
Aug 01 2003
"Gisle Vanem" <giva users.sourceforge.net> wrote in message news:bgeb83$urj$1 digitaldaemon.com..."noobi" <noobi_member pathlink.com> wrote:No, it doesn't. Sorry.char *achar; int code; main() { code = 65; sprintf(achar, "%c", code);I'm amased this doesn't crash (assuming it's a 32-bit program). If it's is a 16-bit program, it shouldn't crash, but give a "NULL pointer assignment" message at program exit. Ref. Borland's c0.asm. Walter, does DMC have a similar __checknull() function?
Aug 02 2003
Hello, I don't know which compiler you have used, however mostly standard compilers will compile the code, but when you try to run the object file, you can see a "Illegal address reference" kind of error, that will abend your running process. Here you have used a variable achar, which is "char *". As we know that if you want to store any data in a pointer variable we need to allocate memory for the data. Here before allocating the memory, you are using the pointer variable to store that "code" data. So here sprintf should traps this out and abend the process. As you have got the output and it is (null), this is might be, because of the compiler. Hope this helps. :-)
Oct 31 2008