Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Question on Pointers (http://www.velocityreviews.com/forums/t441478-question-on-pointers.html)

Zero 02-23-2006 09:46 AM

Question on Pointers
 
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 02-23-2006 09:54 AM

Re: Question on Pointers
 
Zero <chefmuetze@web.de> 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

Reading this might answer your question and clear up your confusion.

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

Ivan Vecerina 02-23-2006 09:54 AM

Re: Question on Pointers
 
"Zero" <chefmuetze@web.de> wrote in message
news:1140687972.885082.277710@g43g2000cwa.googlegr 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 02-23-2006 10:13 AM

Re: Question on Pointers
 
I get confused because I interpret pChar[2] as an address to offset, so
I change the address not the value behind the address.


Zero 02-23-2006 10:30 AM

Re: Question on Pointers
 
Very good hint!


Rod Pemberton 02-23-2006 10:40 AM

Re: Question on Pointers
 

"Zero" <chefmuetze@web.de> wrote in message
news:1140687972.885082.277710@g43g2000cwa.googlegr 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?= 02-23-2006 11:12 AM

Re: Question on Pointers
 
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)

> I change the address not the value behind the address.


Andrey Tarasevich 02-23-2006 05:05 PM

Re: Question on Pointers
 
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 02-23-2006 06:20 PM

Re: Question on Pointers
 
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
header.

Keith Thompson 02-23-2006 07:58 PM

Re: Question on Pointers
 
"Rod Pemberton" <do_not_have@sorry.bitbucket.cmm> 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) kst-u@mib.org <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.


All times are GMT. The time now is 08:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.