Velocity Reviews > Question on Pointers

# Question on Pointers

Zero
Guest
Posts: n/a

 02-23-2006
Hello everybody!

I have a question and need some kind of confirmation!

Is it right that

pChar[2] = 'a' is the same as
*(pChar + 2) = 'a'!

(where char * pChar and pChar = malloc(20))

I get confused with the first example. Why is that?

Zero

Ico
Guest
Posts: n/a

 02-23-2006
Zero <(E-Mail Removed)> wrote:
> Hello everybody!
>
> I have a question and need some kind of confirmation!
>
> Is it right that
>
>
> pChar[2] = 'a' is the same as
> *(pChar + 2) = 'a'!
>
> (where char * pChar and pChar = malloc(20))
>
> I get confused with the first example. Why is that?

Pointer and array equivalance often can be confusing to those new to C.
The comp.lang.c faq has a chapter dedicated to this subject :

http://c-faq.com/aryptr/index.html

--
:wq
^X^Cy^K^X^C^C^C^C

Ivan Vecerina
Guest
Posts: n/a

 02-23-2006
"Zero" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
: Hello everybody!
:
: I have a question and need some kind of confirmation!
:
: Is it right that
:
: pChar[2] = 'a' is the same as
: *(pChar + 2) = 'a'!

yes.
Even the following is equivalent:
2[pChar] = 'a'

: (where char * pChar and pChar = malloc(20))
:
: I get confused with the first example. Why is that?

Why are you getting confused?
Accessing an element at an offset from a pointer
was seen as a frequent enough need to deserve
a friendly notation of its own.
Just as a->x, which is equivalent to (*a).x

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

Zero
Guest
Posts: n/a

 02-23-2006
I get confused because I interpret pChar[2] as an address to offset, so

Zero
Guest
Posts: n/a

 02-23-2006
Very good hint!

Rod Pemberton
Guest
Posts: n/a

 02-23-2006

"Zero" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello everybody!
>
> I have a question and need some kind of confirmation!
>
> Is it right that
>
>
> pChar[2] = 'a' is the same as
> *(pChar + 2) = 'a'!
>
> (where char * pChar and pChar = malloc(20))
>
> I get confused with the first example. Why is that?

Dennis Ritchie in the 1974-5 versions of "C Reference Manual" said the
following:

"The expression ''E1[E2]'' is identical (by definition) to ''* ( (E1) + (
E2 ) ) ''."

and :

"Except for the relaxation of the requirement that E1 be of pointer type,
the expression ''E1->MOS'' is exactly
equivalent to ''(*E1).MOS''."

Rod Pemberton

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a

 02-23-2006
Zero wrote:
> I get confused because I interpret pChar[2] as an address to offset, so

it's also a lookup at that address, not just an offset to an address.
So indeed,pChar[2] is NOT the same as just (pchar + 2), but
*(pchar +2)

Andrey Tarasevich
Guest
Posts: n/a

 02-23-2006
Zero wrote:
>
> I have a question and need some kind of confirmation!
>
> Is it right that
>
>
> pChar[2] = 'a' is the same as
> *(pChar + 2) = 'a'!
>
> (where char * pChar and pChar = malloc(20))

Yes.

> I get confused with the first example. Why is that?

Because that's how the '[]' operator is defined in C language. Saying
'<something>[i]' is just another way of saying '*(<something> + i)' by
definition.

--
Best regards,
Andrey Tarasevich

Default User
Guest
Posts: n/a

 02-23-2006
Zero wrote:

> I get confused because I interpret pChar[2] as an address to offset,
> so I change the address not the value behind the address.

That's ok, we get confused because you don't quote enough for context.
See below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded

Keith Thompson
Guest
Posts: n/a

 02-23-2006
"Rod Pemberton" <(E-Mail Removed)> writes:
[...]
> Dennis Ritchie in the 1974-5 versions of "C Reference Manual" said the
> following:
>
> "The expression ''E1[E2]'' is identical (by definition) to ''* ( (E1) + (
> E2 ) ) ''."

Still true.

> and :
>
> "Except for the relaxation of the requirement that E1 be of pointer type,
> the expression ''E1->MOS'' is exactly
> equivalent to ''(*E1).MOS''."

I'm not sure what he means by "the relaxation of the requirement that
E1 be of pointer type"; I don't believe that clause applies in modern
C.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.