www.digitalmars.com         C & C++   DMDScript  

c++.dos - sprintf with char

reply noobi <noobi_member pathlink.com> writes:
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
next sibling parent reply "Walter" <walter digitalmars.com> writes:
"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
parent reply noobi <noobi_member pathlink.com> writes:
In article <bge9fl$svp$1 digitaldaemon.com>, Walter says...
"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.
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
parent "Walter" <walter digitalmars.com> writes:
"noobi" <noobi_member pathlink.com> wrote in message
news:bgfpf7$2bh6$1 digitaldaemon.com...
 In article <bge9fl$svp$1 digitaldaemon.com>, Walter says...
"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.
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 ?
Write: char achar[2]; achar[0] = code; achar[1] = 0;
Aug 02 2003
prev sibling next sibling parent reply "Gisle Vanem" <giva users.sourceforge.net> writes:
"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
parent "Walter" <walter digitalmars.com> writes:
"Gisle Vanem" <giva users.sourceforge.net> wrote in message
news:bgeb83$urj$1 digitaldaemon.com...
 "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?
No, it doesn't. Sorry.
Aug 02 2003
prev sibling parent raju <raju_member pathlink.com> writes:
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