Pointer question [ Newbie ]

S

sieg1974

Hi,

If I declare a pointer to an integer like this

int *intPtr;

what would &intPtr return?

Thanks,

Andre
 
J

Joona I Palaste

sieg1974 said:
If I declare a pointer to an integer like this
int *intPtr;
what would &intPtr return?

The address of the variable intPtr itself. This is *NOT* the same thing
as the address of the int that intPtr is pointing to.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
 
M

Minti

sieg1974 said:
Hi,

If I declare a pointer to an integer like this

int *intPtr;

what would &intPtr return?


It's not that hard to figure out if you know what & operator is supposed to
"return". Look in your C manual.
 
R

Robert Gamble

Hi,

If I declare a pointer to an integer like this

int *intPtr;

what would &intPtr return?

&intPtr -- the address of the variable intPtr
intPtr -- the address of an integer (not initialized in your example)
*intPtr -- the value of the integer at the address stored in intPtr

Rob Gamble
 
D

Derrick Coetzee

sieg1974 said:
If I declare a pointer to an integer like this
int *intPtr;
what would &intPtr return?

One useful way to think about this is that the following identity always
holds for any addressable expression e:

*(&e) == e
 
D

Dan Pop

In said:
If I declare a pointer to an integer like this

int *intPtr;

what would &intPtr return?

Exactly the same thing as in:

int myint;

where &myint yields the "address" of the variable myint. &intPtr yields
the "address" of the variable intPtr.

Pointers are trickier to the beginner because they have two associated
addresses: the address of the pointed-to object and the address of the
pointer itself. After

intPtr = &myint;

if intPtr is stored at address 0xf00 and myint is stored at address 0xdad
evaluating intPtr will yield 0xdad (represented as a pointer value, not
as an integer value), while &intPtr will yield 0xf00 (again, as a pointer
value).

Making a diagram with pencil and paper might help...

Dan
 
B

Barry Schwarz

And how about ( void ** ) &intPtr ?

Implementation dependent

If a void** can properly hold the value of an int**, then it will
return the same value as &intPtr but coerced into the form of a
void**.

If it cannot, then you have invoked undefined behavior.


<<Remove the del for email>>
 
D

Dan Pop

What's the point of such a conversion? What *exactly* do you expect from
it? The type void ** has none of the nice properties of the type void *
and &intPtr can be safely converted to void *.
Implementation dependent

If a void** can properly hold the value of an int**, then it will
return the same value as &intPtr but coerced into the form of a
void**.

If it cannot, then you have invoked undefined behavior.

AFAICT, undefined behaviour can occur only if the int** value is not
properly aligned for a void**.

Dan
 
B

Barry Schwarz

What's the point of such a conversion? What *exactly* do you expect from
it? The type void ** has none of the nice properties of the type void *
and &intPtr can be safely converted to void *.


AFAICT, undefined behaviour can occur only if the int** value is not
properly aligned for a void**.
What if sizeof(void**) < sizeof(int**)?


<<Remove the del for email>>
 

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,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top