difference between address and pointer

S

somenath

Hi All.

I would like to know the following information.

1)Is there any difference between the address and integer ?

For example suppose int x = 500;
And int y =10;
Suppose address of y is also 500;
My doubt is there any difference between the properties of value of x
and address of y which is 500.

Regards,
Somenath
Int x =500;
 
C

Chris Dollin

somenath said:
I would like to know the following information.

1)Is there any difference between the address and integer ?

Addresses (and pointers) are different from integers.
For example suppose int x = 500;
And int y =10;
Suppose address of y is also 500;

It isn't. 500 is an integer; the address of y is a pointer. They
are two distinguishable types /even if/ the underlying machine uses
integer-like bit-patterns as pointers and integer arithmetic to
do address calculations.
My doubt is there any difference between the properties of value of x
and address of y which is 500.

Yes. For example, it's legal to multiply an integer by another integer,
but not to multiply an address by an integer. Integer values can
be assigned to floating variables; addresses cannot. You cannot
dereference an integer. Etc.
 
M

Mark Bluemel

somenath said:
Hi All.

I would like to know the following information.

1)Is there any difference between the address and integer ?

For example suppose int x = 500;
And int y =10;
Suppose address of y is also 500;
My doubt is there any difference between the properties of value of x
and address of y which is 500.

This question is regularly thrashed over in the group. A little scanning
in Google Groups would probably find the most recent discussion...

Two points for now:-
1) The format of a pointer is not defined by the C standard
2) Not all systems have "flat" address spaces, so the idea that an
address is an integer is not portable.
 
E

Eric Sosman

somenath wrote On 08/09/07 10:12,:
Hi All.

I would like to know the following information.

1)Is there any difference between the address and integer ?

For example suppose int x = 500;
And int y =10;
Suppose address of y is also 500;
My doubt is there any difference between the properties of value of x
and address of y which is 500.

Other respondents have explained that a pointer is
not an integer, by showing that an address is not an
integer. But the difference goes even deeper: a pointer
is not just an address. Contemplate this code fragment
for a while, and you'll understand why a pointer is more
than an address:

union { double d; int i; } u;
double *dp = &u.d;
int *ip = &u.i;

In this code, the two pointers dp and ip point to the
same memory location. Nonetheless, they are different.
What makes them different?
 
C

CBFalconer

somenath said:
I would like to know the following information.

1)Is there any difference between the address and integer ?

For example suppose int x = 500;
And int y =10;
Suppose address of y is also 500;
My doubt is there any difference between the properties of value of x
and address of y which is 500.

Yes. You haven't completed the description of y, which also needs
to specify such things as memory block, whether or not actually in
memory (may be on disk, and swapped out, for example), etc. etc.
POINTERS ARE NOT INTEGERS.
 
M

Malcolm McLean

somenath said:
Hi All.

I would like to know the following information.

1)Is there any difference between the address and integer ?

For example suppose int x = 500;
And int y =10;
Suppose address of y is also 500;
My doubt is there any difference between the properties of value of x
and address of y which is 500.
On the notional van Neumann machine pointers are integers that index the
memory space. Most computers do work like that. But not all. Some processors
have segmented architectures. Some compilers carry around extra information
with pointers to help catch buffer overruns. Siome implement 8 bit bytes on
top of processors which are inherently 32 bit.

So it is not normally helpful to think of a pointer as an integer.
 
K

Keith Thompson

Eric Sosman said:
Other respondents have explained that a pointer is
not an integer, by showing that an address is not an
integer. But the difference goes even deeper: a pointer
is not just an address.
[...]

Except that the standard tends to use the terms "pointer" and
"address" more or less interchangeably. For example, the unary '&'
operator yields the *address* of its operand.

The term "address" is also commonly used to refer to a raw machine
address (the difference is whether it simply refers to a location in
memory, or also has type information associated with it).
 
J

Joe Wright

Eric said:
somenath wrote On 08/09/07 10:12,:

Other respondents have explained that a pointer is
not an integer, by showing that an address is not an
integer. But the difference goes even deeper: a pointer
is not just an address. Contemplate this code fragment
for a while, and you'll understand why a pointer is more
than an address:

union { double d; int i; } u;
double *dp = &u.d;
int *ip = &u.i;

In this code, the two pointers dp and ip point to the
same memory location. Nonetheless, they are different.
What makes them different?
They have different type. They have identical value. They have identical
size (4). They cannot be distinguished from one another except by the
compiler. Why do you think they are different?
 
K

Keith Thompson

Joe Wright said:
Eric Sosman wrote: [...]
Other respondents have explained that a pointer is
not an integer, by showing that an address is not an
integer. But the difference goes even deeper: a pointer
is not just an address. Contemplate this code fragment
for a while, and you'll understand why a pointer is more
than an address:
union { double d; int i; } u;
double *dp = &u.d;
int *ip = &u.i;
In this code, the two pointers dp and ip point to the
same memory location. Nonetheless, they are different.
What makes them different?
They have different type. They have identical value. They have
identical size (4). They cannot be distinguished from one another
except by the compiler. Why do you think they are different?

You said it yourself: they have different type.

Furthermore, how do you know they have the same size, or that their
size is 4 bytes? I work on systems where they'd both have a size of 8
bytes, and it's entirely possible for their sizes to differ.
 
E

Eric Sosman

Joe Wright wrote On 08/09/07 18:01,:
They have different type. They have identical value. They have identical
size (4). They cannot be distinguished from one another except by the
compiler. Why do you think they are different?

Because `array[*ip]' might work (given a suitable
value in `u.i') while `array[*dp]' won't even compile.

Because `ip = dp' won't even compile.

Because `sizeof *ip != sizeof *dp' (probably).

Because `*dp = DBL_MAX' is legal but `*ip = DBL_MAX'
probably invokes undefined behavior.

Because `if ((*dp = DBL_MIN))' branches oppositely
to `if ((*ip = DBL_MIN))'.

Because `printf ("%f\n", *dp)' works (with a suitable
value in `u.d') but `printf ("%f\n", *ip)' invokes undefined
behavior, always.

Because we've moved beyond B.
 
C

CBFalconer

Keith said:
Eric Sosman said:
Other respondents have explained that a pointer is
not an integer, by showing that an address is not an
integer. But the difference goes even deeper: a pointer
is not just an address.
[...]

Except that the standard tends to use the terms "pointer" and
"address" more or less interchangeably. For example, the unary '&'
operator yields the *address* of its operand.

The term "address" is also commonly used to refer to a raw machine
address (the difference is whether it simply refers to a location in
memory, or also has type information associated with it).

Wrong nomenclature IMO. An address is not necessarily an integer.
 
C

CBFalconer

Joe said:
Eric Sosman wrote:
.... snip ...

They have different type. They have identical value. They have
identical size (4). They cannot be distinguished from one another
except by the compiler. Why do you think they are different?

And what gives you the right to make those statements? The
addresses may specify all sorts of added information, such as
memory bank, disk drive (for cached), type of data referenced,
etc. You don't know how the system organizer has arranged all
that.
 
J

Joe Wright

Eric said:
Joe Wright wrote On 08/09/07 18:01,:
They have different type. They have identical value. They have identical
size (4). They cannot be distinguished from one another except by the
compiler. Why do you think they are different?

Because `array[*ip]' might work (given a suitable
value in `u.i') while `array[*dp]' won't even compile.

Because `ip = dp' won't even compile.

Because `sizeof *ip != sizeof *dp' (probably).

Because `*dp = DBL_MAX' is legal but `*ip = DBL_MAX'
probably invokes undefined behavior.

Because `if ((*dp = DBL_MIN))' branches oppositely
to `if ((*ip = DBL_MIN))'.

Because `printf ("%f\n", *dp)' works (with a suitable
value in `u.d') but `printf ("%f\n", *ip)' invokes undefined
behavior, always.

Because we've moved beyond B.
Perhaps I was unclear. Consider please..

void *y, *z;
y = &u.d;
z = &u.i;

Now how shall we differentiate between y and z?
 
E

Eric Sosman

Joe said:
Eric said:
Joe Wright wrote On 08/09/07 18:01,:
Eric Sosman wrote:

somenath wrote On 08/09/07 10:12,:

Hi All.

I would like to know the following information.

1)Is there any difference between the address and integer ?

For example suppose int x = 500;
And int y =10;
Suppose address of y is also 500;
My doubt is there any difference between the properties of value of x
and address of y which is 500.
Other respondents have explained that a pointer is
not an integer, by showing that an address is not an
integer. But the difference goes even deeper: a pointer
is not just an address. Contemplate this code fragment
for a while, and you'll understand why a pointer is more
than an address:

union { double d; int i; } u;
double *dp = &u.d;
int *ip = &u.i;

In this code, the two pointers dp and ip point to the
same memory location. Nonetheless, they are different.
What makes them different?

They have different type. They have identical value. They have
identical size (4). They cannot be distinguished from one another
except by the compiler. Why do you think they are different?

Because `array[*ip]' might work (given a suitable
value in `u.i') while `array[*dp]' won't even compile.

Because `ip = dp' won't even compile.

Because `sizeof *ip != sizeof *dp' (probably).

Because `*dp = DBL_MAX' is legal but `*ip = DBL_MAX'
probably invokes undefined behavior.

Because `if ((*dp = DBL_MIN))' branches oppositely
to `if ((*ip = DBL_MIN))'.

Because `printf ("%f\n", *dp)' works (with a suitable
value in `u.d') but `printf ("%f\n", *ip)' invokes undefined
behavior, always.

Because we've moved beyond B.
Perhaps I was unclear. Consider please..

void *y, *z;
y = &u.d;
z = &u.i;

Now how shall we differentiate between y and z?

They are the same. So are `&u.i' and `(int*)&u'.
But `y' and `z' are different from both `dp' and `ip'.
 
C

CBFalconer

Joe said:
.... snip ...

Perhaps I was unclear. Consider please..

void *y, *z;
y = &u.d;
z = &u.i;

Now how shall we differentiate between y and z?

y is a pointer to a double. z is a pointer to an int. Or rather,
they are such pointers converted to void*, and can only be
accurately converted back to the original types. QED.

For example,

dp = z; <or> ip = y;

are both invalid statements.
 
K

Keith Thompson

CBFalconer said:
Keith said:
Eric Sosman said:
Other respondents have explained that a pointer is
not an integer, by showing that an address is not an
integer. But the difference goes even deeper: a pointer
is not just an address.
[...]

Except that the standard tends to use the terms "pointer" and
"address" more or less interchangeably. For example, the unary '&'
operator yields the *address* of its operand.

The term "address" is also commonly used to refer to a raw machine
address (the difference is whether it simply refers to a location in
memory, or also has type information associated with it).

Wrong nomenclature IMO. An address is not necessarily an integer.

Um, where did I mention integers? What I'm calling a "raw machine
address" refers to a location in memory. Something integer-like is a
common way to implement that, but it's not the only way (except that
it's almost certainly made of bits).
 
R

Richard Heathfield

CBFalconer said:
And what gives you the right to make those statements? The
addresses may specify all sorts of added information, such as
memory bank, disk drive (for cached), type of data referenced,
etc. You don't know how the system organizer has arranged all
that.

All true, but whatever information the system must store in dp to enable
it to locate u.d must *also* be stored in ip to enable it to locate u.i
- so as far as I can see, Joe Wright has a point.

The most contentious part of his statement is that the two pointers
"have identical value" - I see value as being fundamentally bound up
with type, so I don't quite agree that two pointers of different type
can have the same value. Nevertheless I accept that they both point to
the same bunch of bits.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top