Simple Pointer Question

T

trustron

Hi all,



I have got a pointer question. I was told that a pointer is a variable
holding a memory address.



SO:

...

//I: a variable holding a memory address

unsigned char U8 = 0xFFFF;



//II: a variable holding a memory address

unsigned char* pU8 = (unsigned char*)0xFFFF;



printf("U8 = %X \n",U8);

printf("pU8 = %X \n",pU8);

...



Output:

U8 = FF

pU8 = FFFF



I understand that U8 = FF (1 byte). I do not understand why pU8 = FFFF
(2 bytes), there is only 1 byte storage, so where is this number
(FFFF) stored?
 
G

Gianni Mariani

trustron said:
..

//I: a variable holding a memory address

unsigned char U8 = 0xFFFF;

//II: a variable holding a memory address

unsigned char* pU8 = (unsigned char*)0xFFFF;
printf("U8 = %X \n",U8);
printf("pU8 = %X \n",pU8);

Output:

U8 = FF

pU8 = FFFF

I understand that U8 = FF (1 byte). I do not understand why pU8 = FFFF
(2 bytes), there is only 1 byte storage, so where is this number
(FFFF) stored?


pU8 is a memory address (i.e. a pointer). Assigning it to FFFF is
undefined - however on most moderm architectures it is a 32 bit number.
(hence why you are able to assign 0x0000ffff).
 
G

Gary Labowitz

trustron said:
Hi all,



I have got a pointer question. I was told that a pointer is a variable
holding a memory address.
<<snip wrong stuff>>

You were told wrong. A pointer is a variable of type pointer that holds an
address.
A type defines the size of area in memory used to store a value and the
encoding of that value. In the case of a pointer it is an area at least
large enough to store an address for the machine you are on and the encoding
is the address of another area of memory. It is NOT just bits that look like
an address.
There are operations defined for pointer type that only apply to pointer
variables; like the dereferencing operator and pointer arithmetic.
 
J

jeffc

Gary Labowitz said:
You were told wrong. A pointer is a variable of type pointer that holds an
address.

He was not told wrong* - a pointer *is* a variable holding a memory address.
(At least it's supposed to - it could well be argued that 0 is not a memory
address.) He simply wasn't given enough information.

*I've never seen a newsgroup where people are so eager to tell other people
how wrong they are.....
 
A

Andrey Tarasevich

jeffc said:
He was not told wrong* - a pointer *is* a variable holding a memory address.
(At least it's supposed to - it could well be argued that 0 is not a memory
address.) He simply wasn't given enough information.

There are at least four fundamentally different flavors of pointers in
C++: regular pointers to data, pointers to regular functions, pointers
to member functions, pointers to data members. In general case some of
these kinds of pointers will hold a lot more than just a "memory
address". For this reason, in general case it is not correct to say that
a pointer "is a variable holding a memory address".

A pointer holds an address, all right (keeping in mind that in C++
terminology term "address" is a synonym for term "pointer"). Once you
start qualifying that term "address" with such undefined modifiers as
"memory" (as in your "memory address"), you start walking on thin ice.
Needlessly, I might add.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top