当我将指针传递给函数时出现问题 [英] Problem when i pass pointers to functions

查看:53
本文介绍了当我将指针传递给函数时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按引用原则调用是否适用于指针?

有没有办法将指针(通过引用)传递给函数?


这是我的代码:

#include< stdio.h>

#include< stdlib.h>


// f( )应该将num'的值更改为88 ......

void f(int * num)

{

int * num2 = malloc(sizeof(int));

* num2 = 88;


// * num = 88; //数据OUTSIDE f()被更改

num = num2; //数据OUTSIDE函数f()不会改变...


免费(num2);

}


main()

{

int * x = malloc(sizeof(int));

* x = 5;


printf(" x =%d \ n执行f()... \ n",* x);


f(x) ; //调用f()来改变x的值

printf(" x =%d \ n",* x); //错误:X没有改变


免费(x);

}

解决方案

在文章< fh ********** @ registered.motzarella.org> ;, ramif

< ra ****** @ yahoo.co .ukwrote于2007年11月11日星期日晚上9:42:


引用原则调用是否适用于指针?



严格来说,C中没有引用的调用。但是可以用指针值模拟



有没有办法将指针(通过引用)传递给函数?



是的。将指针传递给它们。


int * p;

/ * ... * /

fx(& p) ;

/ * ... * /


这是我的代码:


# include< stdio.h>

#include< stdlib.h>


// f()应该将num'的值更改为88 ...

void f(int * num)

{

int * num2 = malloc(sizeof(int));

* num2 = 88;


// * num = 88; //数据OUTSIDE f()被更改

num = num2; //数据OUTSIDE函数f()不会改变...


free(num2);



这里的回报会更好。


}


main()



隐式int不再合法。只需声明main返回一个int

并在括号之间放置一个''void'来表示它不需要

参数。


{

int * x = malloc(sizeof(int));

* x = 5;


printf(" x =%d \ n执行f()... \ n",* x);


f(x); //调用f()来改变x的值

printf(" x =%d \ n",* x); //错误:X没有改变


free(x);



此处另有回报。


}



你到底想做什么?更改x指向的位置所保留的值

或更改x指向的位置。要做前者f()

可以说:


* x = new_value;


待办事项后者你需要启用f()来对主''''x''进行操作,而不仅仅是

它是本地副本;传递''x'的地址来执行此操作:


f(& x);


/ * in f()* /

* x = new_address;

/ * ... * /


ramif写道:


是否按引用调用原则适用于指针?



是的。 C没有任何参考号码,包括

指针。 (它最接近的是数组在值上下文中衰减为

指针 - 这看起来像是通过

引用调用,但它不是,至少因为没有什么可以用/调用/来调用
。)


有没有办法将指针(通过引用)传递给功能?



No.


您/可以/做的是将/ pointer-to-pointer传递给/。


// f()应该将num'的值更改为88 ...

void f(int * num)



void f(int ** num)


{

int * num2 = malloc(sizeof(int));

* num2 = 88;


// * num = 88; //数据OUTSIDE f()更改



** num = 88;


num = num2 ; //数据OUTSIDE函数f()不会改变...



* num = num2;


免费(num2);



地狱的牙齿,伙计,如果你想让任务转到外面

函数意味着什么,/不要免费`num2` /。


}


main()

{

int * x = malloc(sizeof(int));

* x = 5;


printf(" x =%d \ n执行f()... \ n",* x);


f(x); //调用f()更改x
的值

f(& x);


printf(" x =%d \ n",* x); //错误:X DID NOT CHANGE



现在就可以了。 (当然,你已经把你唯一的指针扔掉了原来商店的商店。哎呀。)


free(x) ;

}



-

Heapy Hedgehog

含义在定义之前。


Richard写道:


ramif< ra ****** @ yahoo .co.ukwrites:


>引用原则调用是否适用于指针?
有没有办法将指针(通过引用)传递给函数?



有人会说参考没有电话。



s / will / was /。


这是真的。



同样如此,关于定义的论据也是如此。


但是误导和混淆几乎所有人。



它让那些已经向他们解释过的人困惑了。

人。


您可以将指针传递给一个对象,该对象适用于所有



最多。


意图并且意义是参考。对于大多数人而言,b $ b的程序员。



确实。


所以对我来说,传递指针确实是通过引用传递的。



这里是定义的东西。不,传递指针

不能通过引用传递(或调用)。你可以通过传递指针来做所有的事情,通过引用来传递所有的东西,/除了/

这样做是隐含的,这是<的定义的一部分br />
传递参考。


无论标准说什么:文字游戏

不明白跨越。



完全正确。你对术语的混淆并不能使C通过 - 参考
,其中必要指针的构造和解引用是隐含的并且是机器的一部分

的语言。 C碰巧有这些结构和

dereferences作为可直接访问的功能,所以它/不需要/ b $ b需要/通过引用传递。


在像(原版)Pascal这样的语言中,你不能用b / b来模拟用其他语言传递引用

功能,所以通过参考传递更加清晰

是一个特定功能。


-

历史刺猬

过去的好时光过去好多了。


Does call by reference principle apply to pointers??
Is there a way to pass pointers (by reference) to functions?

Here is my code:
#include <stdio.h>
#include <stdlib.h>

// f() is supposed to change num''s value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);
}

main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

free(x);
}

解决方案

In article <fh**********@registered.motzarella.org>, ramif
<ra******@yahoo.co.ukwrote on Sunday 11 Nov 2007 9:42 pm:

Does call by reference principle apply to pointers??

Strictly speaking there is no call by reference in C. It can however be
simulated with pointer values.

Is there a way to pass pointers (by reference) to functions?

Yes. Pass pointer to them.

int *p;
/* ... */
fx(&p);
/* ... */

Here is my code:

#include <stdio.h>
#include <stdlib.h>

// f() is supposed to change num''s value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);

A return would be better form here.

}

main()

Implicit int is not legal anymore. Just declare main to return an int
and place a ''void'' between the parenthesis to indicate that it takes no
arguments.

{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

free(x);

And another return here.

}

What exactly do you want to do? Change the value held at the location
pointed to by ''x'' or change what ''x'' points to. To do the former f()
can just say:

*x = new_value;

To do the latter you need to enable f() to act on main''s ''x'' not just
it''s local copy; pass the address of ''x'' to do this:

f(&x);

/* in f() */
*x = new_address;
/* ... */


ramif wrote:

Does call by reference principle apply to pointers??

Yes. C doesn''t have call by reference for anything, including
pointers. (The nearest thing it has is that arrays decay into
pointers in value context -- this can look like call by
reference, but it isn''t, not least because it''s nothing to
do with /calling/.)

Is there a way to pass pointers (by reference) to functions?

No.

What you /can/ do is to pass a /pointer-to-pointer-to/.

// f() is supposed to change num''s value to 88...
void f(int *num)

void f( int **num )

{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed

**num = 88;

num = num2; //data OUTSIDE function f() will not change...

*num = num2;

free(num2);

Hell''s teeth, man, if you want the assignment to outside ths
function to mean anything, /don''t free `num2`/.

}

main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x

f( &x );

printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

It will now. (Of course, you''ve thrown away your only pointer
to the originally mallocated store. Oops.)

free(x);
}

--
Heapy Hedgehog
Meaning precedes definition.


Richard wrote:

ramif <ra******@yahoo.co.ukwrites:

>Does call by reference principle apply to pointers??
Is there a way to pass pointers (by reference) to functions?


Someone will be along to say there is no call by reference.

s/will/was/.

This is kind of true.

As in, true up to arguments about definitions.

But misleading and confuses nearly everyone.

It confuses people who have had it explained to them by confused
people.

You can pass a pointer to an object which for all

Most.

intents and meaning is a "reference" for the greater majority
of programmers.

Indeed.

So for me, passing a pointer is indeed pass by reference.

And here are the definitional things. No, passing a pointer
isn''t pass (or call) by reference. You can do all the things
that pass by reference does by passing pointers, /except/
the doing-it-implicitly that''s part of the definition of
pass-by-reference.

No matter what the standard says : word games
dont get the point across.

Exactly. Your confusion of terms doesn''t make C have pass-
by-reference, in which the construction and dereference of
the necessary pointers is implicit and part of the machinery
of the language. C happens to have those constructions and
dereferences as directly accessible features, so it /doesn''t
need/ to have pass-by-reference.

In a language like (the original) Pascal, you couldn''t
simulate pass-by-reference with the rest of the language
features, and so it was much clearer that pass-by-reference
was a specific feature.

--
Historical Hedgehog
The "good old days" used to be much better.


这篇关于当我将指针传递给函数时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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