473,513 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deciphering ISO C (Chap 6.3.2.3 - Pointers)

Hi,

Forgive me for not searching the entire newsgroup, should this question
already have been answered..

I can't find the logic in why this example doesn't work:

unsigned short* res_16;
unsigned short* pu_16;
unsigned short u_16;

pu_16 = 0x0u;
u_16 = 0x0u;
res_16 = (pu_16 + 0x555u);

The above code yields res_16 == 0xAAA.
I was sort of hoping for 0x555 instead...

Altering the addition line to this:
res_16 = (unsigned short*)(u_16 + 0x555);
corrects the problem...

why?

The platform is a 32bit Intel.
Trying to decipher the addition, I get:
1) 32bit_u = (32bit_u + 32bit_u)
2) 32bit_u = (32bit_u)(16bit_u + 32bit_u)
assuming that the pointer "unsigned short *" is 32bit...

- Philip
Nov 13 '05 #1
2 2239
Philip S wrote:
res_16 = (pu_16 + 0x555u);

The above code yields res_16 == 0xAAA.
I was sort of hoping for 0x555 instead...
No, this makes perfect sense. pu_16 is a pointer to an unsigned short,
which is a 16-bit quantity. Advancing the pointer by 0x555 items will
change the pointer's address by 0xAAA bytes.
The platform is a 32bit Intel.
Trying to decipher the addition, I get:
1) 32bit_u = (32bit_u + 32bit_u)


No, this is not right at all. The pointer is not treated as a 32bit_u in
this operation, at least not in the way you think. When the compiler
sees you adding value 'n' to a pointer, it interprets that to mean you
want make the pointer point 'n' items past where it does now. If the
pointer points to 18-byte structures (for example), then the pointer
will actually be advanced 18*n bytes.

Nov 13 '05 #2
"Philip S" <no@email.addr> writes:
Forgive me for not searching the entire newsgroup, should this question
already have been answered..

I can't find the logic in why this example doesn't work:

unsigned short* res_16;
unsigned short* pu_16;
unsigned short u_16;

pu_16 = 0x0u;
u_16 = 0x0u;
res_16 = (pu_16 + 0x555u);

The above code yields res_16 == 0xAAA.
I was sort of hoping for 0x555 instead...

Altering the addition line to this:
res_16 = (unsigned short*)(u_16 + 0x555);
corrects the problem...


This illustrates the danger of thinking of pointers as integers.

The first assignment, "pu_16 = 0x0u;", assigns a null pointer value to
pu_16. It would be more clearly written as "pu_16 = NULL;" (assuming
"#include <stdlib.h>"). A null pointer is best thought of as a null
pointer. It may or may not be represented as all-bits-zero (it
probably is on your platform).

The expression "pu_16 + 0x555u" is an example of pointer arithmetic.
Adding an integer value N to a pointer yields a pointer that points N
elements (not necessarily bytes) after the original pointer. In this
case, if pu_16 pointed to a sufficiently large array of shorts, res_16
would point to the 0x555th (1365th) element of that array. If the
underlying pointer representation happens to look like an integer byte
index, the result will look like you added 0xAAA to it. Since pu_16
is a null pointer, you're invoking undefined behavior.

What were you planning to do with res_16 anyway? Unless you're
working on an embedded system, it's not likely that there's anything
meaningful at address 0xAAA.

See the section on pointers in your C textbook, and sections 4 and 5
of the C FAQ at <http://www.eskimo.com/~scs/C-faq/top.html>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 13 '05 #3

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

Similar topics

5
5712
by: drdeadpan | last post by:
I asked the DBA to start the Server with options -T1204 and -T3605 and here's what I get. I need help deciphering this. This happens when we have 5 usrs performing concurrent actions and for the...
10
2720
by: Justin Dutoit | last post by:
Hey. I'm still not experienced at error handling, and I need to know if Try.. Catch blocks are meant to be used to handle errors in your own app, ie bugs. Or, are they only for external things like...
12
4059
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
2808
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
2
4737
by: brad | last post by:
Hello all, I'm new to javascript--not too new to a few other programming languages--and I need your help deciphering the Regexp in the following string. Regular expresions are hard enough in...
0
984
by: Jose Cintron | last post by:
I'm working on a program that needs to check the permissions given to a specific user on a specific registry key. The file that I read looks like RegistryKey:username:access As an example ...
25
12989
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
11909
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
9
2590
by: Abu Hamza | last post by:
I have to build a simple login page using CHAP to authenticate users in a DB. Can anyone explain how its done in simple steps? example of code would be great. thx
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
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,...
1
7111
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
7539
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...
1
5095
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...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.