Returning a pointer to a constant string

  • Thread starter =?iso-8859-1?q?Santiago_Urue=F1a?=
  • Start date
K

Keith Thompson

Richard Heathfield said:
Eric Sosman said:
Richard wrote On 09/19/07 12:54,:
[...] in what
cases are &msg[0] and msg not the same in real live systems where they
are 32 or 64 bit pointers?

In all cases.

Not so. &msg[0] and msg are identical when their value is used in an
expression.

When their value is used, yes, but the question was whether they're
the same in general. (The sizes of pointers are irrelevant.)

When they're the operand of a sizeof operator, their values are not
used, but they differ. Likewise for the operand of unary "&"
(but as has been pointed out, '&&msg[0]' is a constraint violation).
 
P

pete

Richard said:
Eric Sosman said:
Richard wrote On 09/19/07 12:54,:
[...]
Since msg is declared as an array, the expression '&msg' is the
address *of the array*, whereas the expression 'msg' (in most
contexts) yields the address of the arrsy's first element,
the same as '&msg[0]'.

I'm a bit sleepy at the moment and understand the above,
but in what
cases are &msg[0] and msg not the same in real live
systems where they
are 32 or 64 bit pointers?

In all cases.

In what cases are the VALUES not the same.

Given: const char msg[] = "Test message";
the answer to your question is that
(&msg[0] == msg) equals one, on all C implementations.
I am not talking the types.
hence I mentioned the pointers or addresses.

He was talking types.

The point he was making is that
though ((char *)&msg == (char *)msg) equals one,
(&msg == msg) is undefined.
 
R

Richard Heathfield

Keith Thompson said:
Richard Heathfield said:
Eric Sosman said:
Richard wrote On 09/19/07 12:54,:
[...] in what
cases are &msg[0] and msg not the same in real live systems where they
are 32 or 64 bit pointers?

In all cases.

Not so. &msg[0] and msg are identical when their value is used in an
expression.

When their value is used, yes, but the question was whether they're
the same in general.

Um, no, the question was: "in what cases are &msg[0] and msg not the same
in real live systems where they are 32 or 64 bit pointers?"

Clearly the pointer-size thing is of no interest to comp.lang.c, since the
cases where &msg[0] and msg are the same or not the same is nothing to do
with pointer size as far as we're concerned. So it resolves to: "in what
cases are &msg[0] and msg not the same?" Nothing in there about "in
general". Eric's answer to this (which he acknowledges is based on a
misreading of what is probably a typo!) was "in all cases", which is
clearly not correct, because there is a case where &msg[0] and msg are the
same.
(The sizes of pointers are irrelevant.)
Agreed.

When they're the operand of a sizeof operator, their values are not
used, but they differ. Likewise for the operand of unary "&"
(but as has been pointed out, '&&msg[0]' is a constraint violation).

Yes, there are indeed cases where they differ, but "in some cases" != "in
all cases".
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:
Richard Heathfield said:
Eric Sosman said:
Richard wrote On 09/19/07 12:54,:
[...] in what
cases are &msg[0] and msg not the same in real live systems where they
are 32 or 64 bit pointers?

In all cases.

Not so. &msg[0] and msg are identical when their value is used in an
expression.

When their value is used, yes, but the question was whether they're
the same in general.

Um, no, the question was: "in what cases are &msg[0] and msg not the same
in real live systems where they are 32 or 64 bit pointers?"

Sorry, I misunderstood your correction to be more general that it
obviously was. (I should spend more time reading and thinking when
something you post looks wrong; it happens, but it's not the way to
bet.)
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top