试图理解函数参数的指针 [英] Trying to understand pointers for function paramaters

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

问题描述

大家好,


我正在努力理解函数参数的指针是如何工作的。因为我理解它,如果你有这样的功能:


void f(int * i)

{

* i = 0;

}


int main()

{

int a;

f(& a);

返回0;

}


它做你想要的(即改变a的值)。

我觉得这不合逻辑。据我所知,a的地址是a。

通过,而* i通过设置这个地址不是i,因为它应该在我的理解中。

我缺少什么?

TIA


PS

在我发布
之前,我已经在小组常见问题解答和其他地方搜索了这个。

解决方案

" Richard Hengeveld" < RI ************** @ hotmail.com>写在

新闻:41 *********************** @ news.wanadoo.nl:

大家好,

我正在努力理解函数参数的指针是如何工作的。正如我理解的那样,如果你有这样的功能:

void f(int * i)
{
* i = 0;
}

int main()
{
int a;
f(& a);
返回0;
}



假设a存在于内存位置0x1000''0000。现在假设''我'是

位于0x1000''0004。


在调用f()之前:

- ------------------

C名记忆。地址。内容(价值位于那里)

------ ------------ ------------------ ------------

a 0x1000''0000:???

i 0x1000''0004:???


在赋值给* i之后的函数f()中:

------------------------ -------------------


C名记忆。地址。内容(价值位于那里)

------ ------------ ------------------ ------------

a 0x1000''0000:0

i 0x1000''0004:0x1000''0000

所以''我'是一个包含''a''地址值的指针。因此

你取消引用''我'通过*我现在指向内存位置

0x1000''0000。因此,如果您修改为0x1000''0000,那么'a''

的值将被修改。


简单,合乎逻辑。


-

- 马克 - >

-


Richard Hengeveld < RI ************** @ hotmail.com>写道:

大家好,

我正在努力理解函数参数的指针是如何工作的。正如我理解的那样,如果你有这样的功能:

void f(int * i)
{
* i = 0;
}

int main()
{
int a;
f(& a);
返回0;
}

它做你想要的(即改变a的价值)。
我发现这不合逻辑。据我所知,a的地址是a。
通过,并且* i通过设置这个地址不是我,因为它应该在我的理解中。
我错过了什么?




是的,你传递的是a的地址功能f。


顺便提一句,i是参数的不良名称。 " I"通常是用作int变量名称的
;参数是指向int的指针。

这是完全合法的,但可能令人困惑。


在作业中


* i = 0;


" i"是指针,并且* i是指针。是一个int对象。您要将

值0分配给int对象。哪个int对象?我指的是,

恰好是a。


如果你写过


i = 0;


你要为i赋值,这是一个指针对象;分配的
值将是一个空指针。


如果更改名称可能会更清楚:


void f(int * ptr_param)

{

* ptr_param = 0;

}


int main(void)

{

int int_object;

f(& int_object);

return 0;

}


(在一个真实的程序中,当然,变量通常应该有名称

,它们反映了他们的'' '用于;对于这个玩具示例,显示他们的类型更重要

。)


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst> ;

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Richard Hengeveld写道:



我是试图理解函数参数的指针是如何工作的。
据我所知,如果你有一个函数:

void f(int * p){
* p = 0 ;
}
int main(int argc,char * argv []){
int a;
f(& a);
返回0;
}

它做你想要的(即改变a的值)。
我发现这不合逻辑。据我所知,
a的地址。通过,
和* p设置这个地址不是p
,因为它应该在我的理解中。




可能令你困惑的是* formal *参数


int * p


这告诉你p是一个指向int类型对象的指针。

声明


p = 0;


会将地址值0指定给p。

表达式


* p


是*参考*(另一个名字)a

所以写作
< br $>
* p = 0;


与写作相同

a = 0;


Hi all,

I''m trying to understand how pointers for function parameters work. As I
understand it, if you got a function like:

void f(int *i)
{
*i = 0;
}

int main()
{
int a;
f(&a);
return 0;
}

It does what you want (namely altering the value of a).
I find this illogical. As far as I can understand, the address of "a" is
passed, and "*i" is set with this address not "i", as it should be in my
understanding.
What am I missing?
TIA

P.S.
I''ve really searched for this in the groups faq and elsewhere before I
posted.

解决方案

"Richard Hengeveld" <ri**************@hotmail.com> wrote in
news:41***********************@news.wanadoo.nl:

Hi all,

I''m trying to understand how pointers for function parameters work. As I
understand it, if you got a function like:

void f(int *i)
{
*i = 0;
}

int main()
{
int a;
f(&a);
return 0;
}



Assume that ''a'' exist at memory location 0x1000''0000. Now assume ''i'' is
located at 0x1000''0004.

Before calling f():
-------------------
C name Mem. Addr. Contents (value located there)
------ ------------ ------------------------------
a 0x1000''0000: ???
i 0x1000''0004: ???

In function f() after the assignment to *i:
-------------------------------------------

C name Mem. Addr. Contents (value located there)
------ ------------ ------------------------------
a 0x1000''0000: 0
i 0x1000''0004: 0x1000''0000

so ''i'' is a pointer that contains the value of the address of ''a''. Thus
weh you dereference ''i'' via *i you now point to memory location
0x1000''0000. So if you modify what is as 0x1000''0000 then the value of ''a''
will be modified.

Simple, logical.

--
- Mark ->
--


"Richard Hengeveld" <ri**************@hotmail.com> writes:

Hi all,

I''m trying to understand how pointers for function parameters work. As I
understand it, if you got a function like:

void f(int *i)
{
*i = 0;
}

int main()
{
int a;
f(&a);
return 0;
}

It does what you want (namely altering the value of a).
I find this illogical. As far as I can understand, the address of "a" is
passed, and "*i" is set with this address not "i", as it should be in my
understanding.
What am I missing?



Yes, you''re passing the address of "a" to the function "f".

Incidentally, "i" is a poor name for the parameter. "i" is commonly
used as a name for an int variable; the parameter is a pointer to int.
It''s perfectly legal, but potentially confusing.

In the assignment

*i = 0;

"i" is a pointer, and "*i" is an int object. You''re assigning the
value 0 to an int object. Which int object? The one i points to,
which happens to be "a".

If you had written

i = 0;

you''d be assigning a value to "i", which is a pointer object; the
value being assigned would be a null pointer.

It might be clearer if you change the names:

void f(int *ptr_param)
{
*ptr_param = 0;
}

int main(void)
{
int int_object;
f(&int_object);
return 0;
}

(In a real program, of course, variables should generally have names
that reflect what they''re used for; for this toy example, it''s more
important to show their types.)

--
Keith Thompson (The_Other_Keith) ks***@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.


Richard Hengeveld wrote:



I''m trying to understand how pointers for function parameters work.
As I understand it, if you got a function like:

void f(int* p) {
*p = 0;
}

int main(int argc, char* argv[]) {
int a;
f(&a);
return 0;
}

It does what you want (namely altering the value of a).
I find this illogical. As far as I can understand,
the address of "a" is passed,
and "*p" is set with this address not "p"
as it should be in my understanding.



Probably what is confusing you is the *formal* argument

int* p

This tells you that p is a pointer to an object of type int.
the statement

p = 0;

would assign the address value 0 to p.
The expression

*p

is a *reference* to (another name for) a
so writing

*p = 0;

is the same thing as writing

a = 0;


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

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