473,385 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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));
Nov 15 '05 #1
5 1516

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.

Nov 15 '05 #2
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.
Nov 15 '05 #3
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.

Nov 15 '05 #4
Me
> 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

Nov 15 '05 #5
On Wed, 06 Jul 2005 18:54:36 -0400, "gyan" <si*****@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
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.