指针讨论 [英] Pointer discussion

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

问题描述

大家好,


我无法理解以下代码的区别:


第一个例子


void func(char ** ch)

{

char * ptr;

ptr = new char;

* ptr =''A'';

* ch = ptr;

}


无效main()

{

char * ch = NULL;

func(& ch);

printf ("%c",* ch);

删除ch;

getch();

}

输出:给出A.正确的实现。

第二个例子


void func(char * ch)

{

char * ptr;

ptr = new char;

* ptr =''A'';

ch = ptr ;

}


void main()

{

char * ch = NULL;

func(ch);

printf("%c",* ch);

删除ch;

getch();

}

输出:编译但不提供任何输出。好像要休息了,

给出了一个运行时错误。


任何人都可以解释这些情景吗?...


提前致谢

Harry

Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr=''A'';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr=''A'';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry

推荐答案

Harry写道:
Harry wrote:
大家好,

我无法理解以下代码的区别:

第一个例子

void func(char ** ch)
{
char * ptr;
ptr = new char;
* ptr =''A'';
* ch = ptr;
}

void main()
{* * char * ch = NULL;
func(& ch);
printf("%c",* ch );
删除ch;
getch();
}
输出:给出A.正确的实现。
第二个例子

void func (char * ch)
{
char * ptr;
ptr = new char;
* ptr =''A'';
ch = ptr;
}

void main()
{* * char * ch = NULL;
func(ch);
printf("%c" ,* ch);
删除ch;
getch ();
}
输出:编译但不提供任何输出。似乎打破并且
给出了运行时错误。

任何人都可以解释这些情景吗?...

提前致谢
Harry
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr=''A'';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr=''A'';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry




void foo(int i)

{

i = 1;

}


void foo(int * i)

{

* i = 1;

}


你看到了区别吗?


-

问候,

Slava



void foo(int i)
{
i = 1;
}

void foo(int *i)
{
*i = 1;
}

Do you see the difference?

--
Regards,
Slava


您可以通过用int替换char *来简化您的问题:

void funcA(int * i)

{

* i = 10;

}


void funcB(int i)< br $>
{

i = 20;

}


int main()

{

int i = 0;

funcA(& i);

funcB(i);

返回0;

}


对funcA的调用会改变主要的我,就像你的第一个电话一样

按预期工作。然而,对funcB的调用并没有改变''我'

in main(在你的情况下会导致运行时错误,当你

dereference'时) 'ch'',空指针)。为什么?因为所有的funcB都是

正在改变''我'的副本,就像你的第二个程序是

只替换你的''ch''的副本第二个函数。


(另请注意,main应该返回一个int,而不是void,尽管你的编译器可以接受的是
。)


Harry写道:
You can simplify your problem by replacing char* with int:

void funcA(int* i)
{
*i = 10;
}

void funcB(int i)
{
i = 20;
}

int main()
{
int i = 0;
funcA(&i);
funcB(i);
return 0;
}

The call to funcA changes ''i'' in main, just like your first call
works as expected. The call to funcB, however, doesn''t change ''i''
in main at all (and in your case causes a runtime error when you
dereference ''ch'', a null pointer). Why? Because all funcB is
doing is altering a copy of ''i'', just like your second program is
only alterning a copy of ''ch'' in your second func.

(Also note that main should return an int, not void, despite what
your compiler may accept.)

Harry wrote:
大家好,

我无法理解以下代码的区别:

第一个例子

void func(char ** ch)
{
char * ptr;
ptr = new char;
* ptr =''A'' ;
* ch = ptr;
}


{*> char * ch = NULL;
func(& ch);
printf("%c",* ch);
删除ch;
getch();
}
输出:给出A.正确的实现。
第二个例子

void func(char * ch)
{
char * ptr;
ptr = new char;
* ptr =''A'';
ch = ptr;
}


{
char * ch = NULL;
func(ch);
printf("%c",* ch);
删除ch;
getch();
}
输出:编译但不提供任何输出。似乎打破并且
给出了运行时错误。

任何人都可以解释这些情景吗?...

提前致谢
Harry
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr=''A'';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr=''A'';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry



在你的第二个例子中,你发送func a null-ptr,你重新分配给

指向你新创建的char。在将指针区域分配为A之后,你需要为本地参数指针分配该指针的副本,但这是和/或
不会传播回你的调用功能。从主要你仍然

有你分配的本地null-ptr。然后你取消引用你的

null-ptr并且有未定义的行为。


-

问候// Bo

" Harry" < AR ****** @ fht-esslingen.de>在消息中写道

新闻:f9 ************************** @ posting.google.c om ...
In your second example you are sending func a null-ptr with you reassign to
point to your newly created char. After assinging the pointer area a ''A'' you
assign your local parameter pointer a copy of that pointer but this is and
will not be propagated back to your calling function. From main you still
have the local null-ptr as you assigned it. You then dereference your
null-ptr and that has undefined behaviour.

--
Regards // Bo
"Harry" <ar******@fht-esslingen.de> wrote in message
news:f9**************************@posting.google.c om...
大家好,

我无法理解以下代码的区别:

第一个例子

void func(char ** ch)
{
char * ptr;
ptr = new char;
* ptr =''A'';
* ch = ptr ;




{* * char * ch = NULL;
func(& ch);
printf( "%c",* ch);
删除ch;
getch();
}
输出:给出A.正确的实现。
第二个例子

void func(char * ch)
{
char * ptr;
ptr = new char;
* ptr =''A'';
ch = ptr;
}


{*> char * ch = NULL;
func(ch);
printf("%c",* ch);
删除ch;
getch();
}
输出:编译但不提供任何输出。似乎打破并且
给出了运行时错误。

任何人都可以解释这些情景吗?...

提前致谢
Harry
Hi all,

I am unable to understand the difference in the following codes:

First Example

void func(char **ch)
{
char *ptr;
ptr=new char;
*ptr=''A'';
*ch=ptr;
}

void main()
{
char *ch=NULL;
func(&ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Gives A. Correct implementation.
Second example

void func(char *ch)
{
char *ptr;
ptr=new char;
*ptr=''A'';
ch=ptr;
}

void main()
{
char *ch=NULL;
func(ch);
printf("%c",*ch);
delete ch;
getch();
}
Output: Compiles but does does not give any output. Seems to break and
gives a run time error.

Can anyone explain the scenarios??...

Thanks in advance
Harry



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

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