|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
c++.dos - 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 ?
Aug 01 2003
"noobi" <noobi_member pathlink.com> wrote in message news:bgdrgp$f8i$1 digitaldaemon.com... 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... 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..."noobi" <noobi_member pathlink.com> wrote in message news:bgdrgp$f8i$1 digitaldaemon.com... Aug 02 2003
"noobi" <noobi_member pathlink.com> wrote: Aug 01 2003
"Gisle Vanem" <giva users.sourceforge.net> wrote in message news:bgeb83$urj$1 digitaldaemon.com..."noobi" <noobi_member pathlink.com> wrote: 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
|