Thread overview |
---|
August 01, 2003 sprintf with char | ||||
---|---|---|---|---|
| ||||
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 ? |
August 01, 2003 Re: sprintf with char | ||||
---|---|---|---|---|
| ||||
Posted in reply to noobi | "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. |
August 01, 2003 Re: sprintf with char | ||||
---|---|---|---|---|
| ||||
Posted in reply to noobi | "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. # rm /bin/laden /bin/laden: Not found |
August 02, 2003 Re: sprintf with char | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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 ? |
August 03, 2003 Re: sprintf with char | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gisle Vanem | "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. |
August 03, 2003 Re: sprintf with char | ||||
---|---|---|---|---|
| ||||
Posted in reply to noobi | "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; |
October 31, 2008 Re: sprintf with char | ||||
---|---|---|---|---|
| ||||
Posted in reply to noobi | 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. :-) |
Copyright © 1999-2021 by the D Language Foundation