473,405 Members | 2,338 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,405 software developers and data experts.

Can any body explain the output of following code

static char buf[10] = {1,2,3,4,5,6,7,8};
printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1));

Thanks

Vija
Jul 19 '05 #1
14 2431

"srikanth" <ge********@yahoo.com> wrote in message
news:be**********@news.mch.sbs.de...
static char buf[10] = {1,2,3,4,5,6,7,8};
printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1));

Thanks

Vija


The output of this code is completely undefined by the C++ standard. On my
computer it produced

Address: 4390348 : Val = 134678021

I don't see anything unusual there.

Perhaps if you say what it produces on your computer and why you find it
hard to understand then someone wil be able to help.

Perhaps if you convert 134678021 to hex (its 0x08070605) you will understand
better what is happening.

john
Jul 19 '05 #2
"srikanth" <ge********@yahoo.com> wrote in message
news:be**********@news.mch.sbs.de
static char buf[10] = {1,2,3,4,5,6,7,8};
printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1));

Thanks

Vija


You should:

1. Say what output you are seeing, because this may be system dependent (and
is in this case).

2. Say what puzzles you about it so that we don't have to engage in guessing
games.
In the expression

(int*)buf+1

the cast operator has a higher precedence than the + operator, so buf is
first cast to a pointer-to-int and then 1 is added to this pointer to int,
using pointer arithmetic. For any type T, adding 1 to a pointer-to-T returns
the pointer value plus sizeof(T). Thus you should get the address of buf
plus sizeof(int).

In the expression

*((int*)buf+1)

you are dereferencing the last expression. Assuming sizeof(int)==4,

(int*)buf+1

will equal the address of buf[4] and the int that you get from dereferencing
it is formed from

buff[4]-buf[7],

i.e., from the values 5,6,7 and 8. What this int looks like depends on
whether your computer uses a little endian or big endian system. Intel
processors use the little endian system, which means that lower addresses
supply the less significant digits.

Thus the four digits are read as:

0x08070605

which equals

134678021 decimal.

If you have a computer that uses the big endian system, then the digits are
read as

0x05060708

which equals

84281096

decimal.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #3
Thanks John,
Now I understood it well
Do you know any compiler which follows Big endian format
Regards
Vijay
Jul 19 '05 #4

vijay wrote:

Thanks John,
Now I understood it well
Do you know any compiler which follows Big endian format


http://mindprod.com/jgloss/endian.html
(What Sex Is Your CPU?)

regards,
alexander.

--
"The endian-agnostic Power-PC's have a foot in both camps. They
are bisexual, but the OS usually imposes one convention or the
other. e.g. Mac PowerPCs are big-endian. "
Jul 19 '05 #5
Ya ,compilers was a typing error,
well ,why there is a padding of zero for evey hexadecimal number ie
0x08070605

Vijay
Jul 19 '05 #6
I think I got the answer
1 Byte can have 2 power 8: 256 characters combinatins:. 2 hexadecimal
digits,(16X16)
Thanks
vijay

"vijay" <ge********@yahoo.com> wrote in message
news:be**********@news.mch.sbs.de...
Ya ,compilers was a typing error,
well ,why there is a padding of zero for evey hexadecimal number ie
0x08070605

Vijay

Jul 19 '05 #7

vijay wrote:

Ya ,compilers was a typing error,
well ,why there is a padding of zero for evey hexadecimal number ie
0x08070605


vijay, are you studying some CS Ph.D questionary, or what?

regards,
alexander.
Jul 19 '05 #8

"vijay" <ge********@yahoo.com> wrote in message
news:be**********@news.mch.sbs.de...
Ya ,compilers was a typing error,
well ,why there is a padding of zero for evey hexadecimal number ie
0x08070605


Remember nibble?
A nibble is half of an eight bit byte.
So how do you represent 5 in a byte - 0x05.
Jul 19 '05 #9
"vijay" <ge********@yahoo.com> wrote in message
news:be**********@news.mch.sbs.de
Ya ,compilers was a typing error,
well ,why there is a padding of zero for evey hexadecimal number ie
0x08070605

Vijay


I am assuming that each byte is 8 bits. Taking the example of the number 5,
it is stored in 8 bits as

00000101

If you are just considering one of these numbers in isolation, then the
leading zeros don't matter. But if you are laying them end to end, then the
leading zeros become internal zeros and affect the significance of other
digits, so you can't leave them out.

Each hexadecimal digit takes 4 bits, so two are required per byte. 0x05 is
precisely

00000101

as required.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 19 '05 #10
Alexander Terekhov wrote:
vijay wrote:
Thanks John,
Now I understood it well
Do you know any compiler which follows Big endian format

http://mindprod.com/jgloss/endian.html
(What Sex Is Your CPU?)

regards,
alexander.

--
"The endian-agnostic Power-PC's have a foot in both camps. They
are bisexual, but the OS usually imposes one convention or the
other. e.g. Mac PowerPCs are big-endian. "


FYI, The ARM processors can be configured either Big Endian
or Little Endian.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #11

Thomas Matthews wrote:
[...]
"The endian-agnostic Power-PC's have a foot in both camps. They
are bisexual, but the OS usually imposes one convention or the
other. e.g. Mac PowerPCs are big-endian. "


FYI, The ARM processors can be configured either Big Endian
or Little Endian.


Yeah. And since both PPC and ARM are clearly infringing upon SCO's
IP (UNIX "methods and concepts"... with respect to CPU bisexuality,
you know), they will be declared "illegal" by SCO at the next press
conference. SCO will rightfully demand that all PPC and ARM devices
shall be immediately unplugged and be prepared for destruction
(pending licensing negotiations outcome).

regards,
alexander.

--
http://www.ibm.com/servers/eserver/linux/fun
Jul 19 '05 #12

"Bruce" <br***@nospam.com> wrote in message news:3f*************@news.dallas.sbcglobal.net...
In comp.lang.c++
"srikanth" <ge********@yahoo.com> wrote:
static char buf[10] = {1,2,3,4,5,6,7,8};
printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1));


I assume this is for fun or learning or something? If you wrote code like
that where I work, well, you probably wouldn't work there very long.


Won't work on any platform where ints and pointers aren't coincidentally compatible.
If you want to dump pointers out with printf use %p and cast it to void* first.
Jul 19 '05 #13
HI
ANy specific reason for the other output ???
static char buf[10] = {1,2,3,4,5,6,7,8,};
static char buf1[10]={'1','2','3','4','5','6','7','8'};
printf("Address BUF: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1));
//hex conversion of output 8070605
printf("Address BUF1: %u : Val = %d
\n",(int*)buf1+1,*((int*)buf1+1)); //38373635 hex conversion of output


"Josephine Schafer" <js@usa.net> wrote in message news:<be************@ID-192448.news.dfncis.de>...
"vijay" <ge********@yahoo.com> wrote in message
news:be**********@news.mch.sbs.de...
Ya ,compilers was a typing error,
well ,why there is a padding of zero for evey hexadecimal number ie
0x08070605


Remember nibble?
A nibble is half of an eight bit byte.
So how do you represent 5 in a byte - 0x05.

Jul 19 '05 #14
The first case the the digits are stored directly as signed integers.
The second case they are stored as characters, ie coverting them to ascii
values and then storing them

Tyr adding the key word unsigned as characters are by default signed
static char buf[10] = {128,2,3,4,5,6,7,8};
if try to print the first char then output will be -1
if
static unsigned char buf[10] = {128,2,3,4,5,6,7,8};
then each value can take up to 255
The other array you will not be able to store even 2 digit number

Regards
vijay
Jul 19 '05 #15

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

Similar topics

16
by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails in the testwwws users mailbox. As you can see I...
2
by: Bender | last post by:
Hi, I am wanting to capture an onmousedown event without firing the body tags onload event. Also, if anyone could explain why this happens that would be excellent. I can't see how an...
2
by: eczino | last post by:
I currently have a web form posting back to a SQL table using a Stored Procedure. Part of this SP is that it pulls data from another table and inserts a new row into the registration table. I...
14
by: Ina Schmitz | last post by:
Hello all, I don't succeed in displaying the explain plan. I use IBM DB2 Universal Database 8.2. I tried to do the example given in the online help for "Visual Explain". The tables...
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
2
by: =?Utf-8?B?TWljaGFlbCBkZSBWZXJh?= | last post by:
To all, I was wondering if someone has sample VB.NET code that will help me send rows of a gridview into a body section of an e-mail. My goal is to present multiple rows within a gridview to a...
5
by: kabotnet | last post by:
Hi, I'm new in db2, I'm trying to execute EXPLAIN command on some queries but i have error like: And message similar to: Token EXPLAIN is not valid, valid tokens ( END GET SET CALL DROP FREE...
7
by: Mike Kent | last post by:
It's often useful for debugging to print something to stderr, and to route the error output to a file using '2>filename' on the command line. However, when I try that with a python script, all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.