![]() |
char * and int *
why following's output is noting in C program
int a=5; printf("\n%s\n",(char *)(&a)); and following lines give correct output printf("\n%d\n",*(int *)(&a)); |
Re: char * and int *
gyan wrote: > why following's output is noting in C program > > int a=5; > printf("\n%s\n",(char *)(&a)); Read up on the conversion specifiers a bit. Specifically, from the man page of printf(), I'll quote (partly) a couple that might be of interest to you: s The char * argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating NUL charac- ter;... p The void * pointer argument is printed in hexadecimal (as if by `%#x' or `%#lx'). So `s' is not the correct thing to use when you are trying to print out the address (provided, I understand you correctly), rather p is. > > and > following lines give correct output > > printf("\n%d\n",*(int *)(&a)); is the same as printf("\n%d\n",a); and prints 5 with a newline before and after the number.The cast is superfluous. |
Re: char * and int *
gyan wrote:
> why following's output is noting in C program > > int a=5; > printf("\n%s\n",(char *)(&a)); `%s` wants a string, ie a char* pointer to a null-terminated sequence of characters. `a`, whose address you have taken and then forcibly (but reversibly) converted to `char*`, isn't any sequence of characters at all, so whatever you get out isn't C's responsibility any more - it will be an accident of the implementation. (Or even a deliberate feature, but usually not.) *As it happens*, the underlying implementation may not care. There are some bytes in the object `a`; it may treat them as characters and print until it finds a zero byte. If you machine is big-endian, or the character with value 5 is invisible, you'll get what you got - no visible characters displayed. > and > following lines give correct output > > printf("\n%d\n",*(int *)(&a)); Because you ask to print an int, and what you give it is *(int *)(&a) which is *(&a) because the cast is redundant, &a already being int*, and *&a === a by the co-definitions of * and &. -- Chris "electric hedgehog" Dollin It's called *extreme* programming, not *stupid* programming. |
Re: char * and int *
I agree with you all, but i am playing little bit.
see i am working on SunOS au1646s 5.8 Generic_117000-01 sun4u sparc SUNW,Sun-Fire-15000 here int takes 4 bytes. Now i have prgram: int a=6; char *tmp; tmp=(char *)(&a); *(tmp+4)='\0'; printf("\n%s\n",tmp+3); Now i run it through debugger dbx. according to output: p *(tmp) = '\0' p *(tmp+1) = '\0' p *(tmp+2) = '\0' p -f%d *(tmp+3) = 6 p *(tmp+3) = '\0' So when i write printf("\n%s\n",tmp+3); i hope that program should see 6 at that adress (tmp+3) and '\0' at next address. Now %s should print 6, since i am passing a char * who stores a char array type having 6 at oth byte and '\0' at 1st byte. |
Re: char * and int *
> why following's output is noting in C program
> > int a=5; > printf("\n%s\n",(char *)(&a)); Odds are you're on a big endian endian machine that read the upper 8 value bits (which are 0 in this case) of |a| and printed nothing because it treated it as a 0 length NUL terminated string. > and > following lines give correct output > > printf("\n%d\n",*(int *)(&a)); *(int*)&a is the same thing as a |
Re: char * and int *
On Wed, 06 Jul 2005 18:54:36 -0400, "gyan" <singhg4@anz.com> wrote in
comp.lang.c: > I agree with you all, but i am playing little bit. > see i am working on > SunOS au1646s 5.8 Generic_117000-01 sun4u sparc SUNW,Sun-Fire-15000 > > here int takes 4 bytes. > Now i have prgram: > int a=6; > char *tmp; > tmp=(char *)(&a); > *(tmp+4)='\0'; If sizeof(int) is four or less on your platform, the line above invokes undefined behavior. And if sizeof(int) is not 4 on your platform, it makes no sense at all. > printf("\n%s\n",tmp+3); The problem is that once you have produced undefined behavior, and you have, the rest of the program is completely meaningless as far as the C standard, and therefore this newsgroup, are concerned. > > Now i run it through debugger dbx. Debuggers are completely off topic here. > > So when i write > printf("\n%s\n",tmp+3); > i hope that program should see 6 at that adress > (tmp+3) and '\0' at next address. > Now %s should print 6, since i am passing a char * who stores a char array > type having 6 at oth byte and '\0' at 1st byte. No it shouldn't, you are mistaken. The character '6' in your implementation's character set will print "6". The binary value 0x6 will not. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html |
| All times are GMT. The time now is 09:43 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.