difference

H

Hallvard B Furuseth

Ramkumar said:
what is the difference between objects and pointers?

An object is 'a region of data storage in the execution environment, the
contents of which can represent values'. (Quoted from the standard.)
E.g. an integer, struct, malloced area, pointer, or whatever.
So 'int i;' declares an object (and names it 'i').

A pointer is an object which contains the address of another object (or
of a function). E.g. `int *p;'. If you set p = &i, you can access `i'
either through its name `i' or by following the pointer (with the `*'
operator): `*p'.
 
M

Mike Wahler

Ramkumar R K said:
what is the difference between objects and pointers?

A pointer *is* an object. The difference between objects
of pointer type and those of other types, is that pointer types
are used for storing addresses, and other types store other
types of data, e.g. 'int', 'float', a user defined type
(a class or struct), etc.

-Mike
 
R

Richard Bos

what is the difference between objects and pointers?

A pointer is an object, an object is not necessarily a pointer.
You can have pointer values, which are values of pointer objects, but it
makes little sense to talk of an object value (as opposed to <sometype>
value or simply value).
Pointers can point at pointers, but objects cannot object to objects.

Richard
 
R

Richard Bos

pete said:
The result of the & operator, is a pointer, though not an object.

My hovercraft, OTOH, is full of eels - which is just as relevant.

Richard
 
R

Richard Bos

Richard Heathfield said:
Not necessarily. In this code fragment:

int i;
int *p = &i;

&i is a pointer, but not an object.

AFAICT, &i is an expression with pointer type, evaluating to a pointer
value, but p is the only pointer.

Then again, it's a pain to search a document for the definition of
"pointer, but not pointer type or pointer value or object of pointer
type", but the C99 Standard _seems_ to use "pointer" both in contexts
where it must mean "a value of a pointer object" (conversion) and in
contexts where it must mean "an object of pointer type" (increment), so
if _they_ don't have to be consistent about it, I don't see why we
should.

Richard
 
R

Richard Heathfield

Richard said:
A pointer is an object,

Exceptions include the value yielded by the & "address-of" operator, and the
unadorned name of a function (which is converted to a pointer to the
address of that function, but AFAICT is not an object).
 
P

pete

Richard said:
My hovercraft, OTOH, is full of eels - which is just as relevant.

That the result of the & operator, is a pointer, and not an object,
is completely relevant to OP's question of
"what is the difference between objects and pointers?"
when OP is being told that pointers are objects.
A pointer isn't necessarily an object.
Pointer is a catagory of types, with each type of pointer
being a pointer to some other type.
All constants are of object type also, not just objects.
 
H

Hallvard B Furuseth

pete said:
The result of the & operator, is a pointer, though not an object.

Are you sure? Where does the standard say this?

The program has to put the result somewhere - maybe in memory, maybe in
a register. That place isn't named by a variable or anything else, but
I don't see what that has to do with it. (Or the value could be
optimized away, of course, but so can other objects.)
 
P

pete

Hallvard said:
Are you sure? Where does the standard say this?

The program has to put the result somewhere
- maybe in memory, maybe in a register.

The result does not imply storage according
to the representation of the type,
on the abstract machine, and that's why it's not an object.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
Not necessarily. In this code fragment:

int i;
int *p = &i;

&i is a pointer, but not an object.

As I undersdand it, &i is a pointer constant (like NULL in a pointer
context, or the name of an array). It's not a object because it's a constant
value. Constant values have no address. Hence, the assertion "a pointer is an
object" sounds good to me. Please, let me know if I'm wrong.
 
T

Thomas Stegen

Hallvard said:
Are you sure? Where does the standard say this?

I think it falls down to the fact the standard does not say to
much explicitly, but rather on how it uses the term.

Me and Joe Wright had a discussion about this some time back.

If you want to see the arguments made then Message id of starting post
is: <[email protected]>
 
C

CBFalconer

Emmanuel said:
As I undersdand it, &i is a pointer constant (like NULL in a
pointer context, or the name of an array). It's not a object
because it's a constant value. Constant values have no address.
Hence, the assertion "a pointer is an object" sounds good to me.
Please, let me know if I'm wrong.

And, in the above context:

int *p = c & i;

is probably a silly error, due to the appalling overuse and
overloading of symbols in C in context sensitive manners. We
won't fix this now, but I just thought I would rant and rave a
bit. Wasn't it the Queen of Hearts who said "it means just what I
intend it to mean".
 
J

Joe Wright

Ramkumar said:
what is the difference between objects and pointers?

comp.lang.c is not a substitute for reading your C book.

what is the difference fruit and apples?
 
J

Joe Wright

Richard said:
Exceptions include the value yielded by the & "address-of" operator, and the
unadorned name of a function (which is converted to a pointer to the
address of that function, but AFAICT is not an object).
Don't fight it so hard. "pointer to the address of" indeed. I contend
simply that we confuse "address" and "pointer" too often. An address is
a value with pointer type but it is a value, not an object. The value
yielded by the & operator is an address. Calling it a pointer is wrong.
Expressing the name of a function yields the address of it. Not a
pointer.

We all know that C is value oriented. We pass arguments to functions by
value. Functions return values to their callers. But we 'say' it wrong.
Because the value returned is of pointer type we call it 'pointer'
instead of value. Example..

int *p; /* p is a pointer to int */
p = malloc(N * sizeof *p);

The return from malloc() is a value of type (void*). The value is
assigned to p, the only pointer around.

I commend the reader to the first sentence of Chapter 5 of K&R 1 and 2.
"A pointer is a variable that contains the address of another variable."

What's so hard about this?
 
R

Richard Heathfield

Joe said:
Don't fight it so hard. "pointer to the address of" indeed.

Sorry about that. I meant, of course, "a pointer to that function".
I contend
simply that we confuse "address" and "pointer" too often. An address is
a value with pointer type but it is a value, not an object. The value
yielded by the & operator is an address. Calling it a pointer is wrong.

I agree that the value is an address, but disagree that calling it a pointer
is wrong. The Standard says:

"The unary & operator returns the address of its operand. [...]
the result is a pointer to the object or function designated by its
operand."
Expressing the name of a function yields the address of it. Not a
pointer.

See above.

<snip>
 
A

Arthur J. O'Dwyer

Don't fight it so hard. "pointer to the address of" indeed.

We all have our terminological quirks. Indeed, I would contend that
Richard's use of "pointer to the address of" is silly -- a pointer,
to me, *is* an address (or, equivalently, the value of a pointer
object is an address). A pointer points to an object, not an address!
:) Obviously, SMV [someone's mileage varies], or it could have been
a thinko.
I contend simply that we confuse "address" and "pointer" too often.
An address is a value with pointer type but it is a value, not an
object. The value yielded by the & operator is an address.

Agreed.
Calling it a pointer is wrong.

Disagreed. (Would you object to my calling 5 an 'int', even though
it's not an object? ...And that's not even getting into the debate
that hit c.s.c a year or so ago, when it seemed like maybe 5 *was*
an object, just not an lvalue... I don't remember the details.)
Expressing the name of a function yields the address of it. Not a
pointer.

Just as many people call the value 5 an int, many people will
continue to call &foo a pointer no matter how consistent it might
be to go the other way. :)
We all know that C is value oriented. We pass arguments to functions by
value. Functions return values to their callers. But we 'say' it wrong.
Because the value returned is of pointer type we call it 'pointer'
instead of value.

Exactly.
I commend the reader to the first sentence of Chapter 5 of K&R 1 and 2.
"A pointer is a variable that contains the address of another variable."

What's so hard about this?

I'm actually pleasantly surprised that K&R1 Ch5 manages to keep
the concepts of 'pointer object' and 'address value' separate as much
as they do. Way to go, K&R! But I don't like how they appropriated
the word "pointer" to describe those pointer objects, because, as you've
observed above, we *do* use the word "pointer" to refer to values of
pointer type.

Bottom line: Avoid arguing over the semantics of "pointer." Stick
to "object" and "value" in pedantic contexts, because they have almost
unambiguous meanings in C.

-Arthur
 

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,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top