关于指针的问题 [英] Question on Pointers

查看:76
本文介绍了关于指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好!


我有疑问,需要某种确认!


是不是

pChar [2] =''a''与

*(pChar + 2)=''a''相同!


(其中char * pChar和pChar = malloc(20))


我对第一个例子感到困惑。为什么?




解决方案

零< ch **** ****@web.de>写道:

各位大家好!

我有一个问题,需要某种确认!

是不是

pChar [2] =''a''与
相同*(pChar + 2)=''a''!

(其中char * pChar和pChar = malloc (20))

我对第一个例子感到困惑。为什么会这样?




指针和数组的等效通常会让那些新来的人感到困惑。

comp.lang.c faq有专门讨论该主题的章节:

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


阅读此内容可能会回答您的问题并消除您的困惑。


-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C


"零" < CH ******** @ web.de>在消息中写道

新闻:11 ********************** @ g43g2000cwa.googlegr oups.com ...

:各位大家好!



:我有一个问题,需要某种确认!



:是不是



:pChar [2] =''a''与

相同: *(pChar + 2)=''a''!


是的。

即使以下是相同的:

2 [pChar] =''a''


:(其中char * pChar和pChar = malloc(20))



:我对第一个例子感到困惑。为什么会这样?


为什么你会感到困惑?

从指针偏移处访问元素

被视为经常需要得到自己的友好符号

a
就像a-> x,相当于(* a).x

Ivan

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++<> http://www.brainbench.com

我感到困惑,因为我将pChar [2]解释为偏移地址,所以

我改变地址而不是地址后面的值。


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

解决方案

Zero <ch********@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


"Zero" <ch********@web.de> wrote in message
news:11**********************@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


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


这篇关于关于指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆