代码问题 [英] problem with the code

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

问题描述



以下代码有什么问题吗?


问候


char * func ()

{

char c;

return(& c);

}

#include< stdio.h>

int main()

{

char * ch;

ch = func();

* ch =''A'';

pritnf("%c",* ch);

}

解决方案

8月25日上午11点22分,ramu< ramu .... @ gmail.comwrote:




以下代码有什么问题吗?


问候


char * func()

{

char c;

return(& c);}


#include< stdio.h>

int main()

{

char * ch;

ch = func();

* ch =''A'';

pritnf("%c",* ch) ;


}


pritnf("%c",* ch);


>抱歉。它应该是printf(%c,* ch);


8月24日晚上11点22分,ramu< ramu .... @ gmail.comwrote:
< blockquote class =post_quotes>


以下代码有什么问题吗?


问候

char * func()

{

char c;

return(& c);}


#include< stdio.h>

int main()

{

char * ch;

ch = func();

* ch =''A'';

pritnf("%c",* ch); < br $>

}



看起来像家庭作业。如果你解释是否*你认为

有问题,为什么或为什么不这样做呢?并且不要忘记,当你提交作业时,要确认并引用那些回复你的人的帖子



On Sun,2008年8月24日23:22:01 -0700(PDT),ramu< ra ****** @ gmail.com>

写道:


>

以下代码有什么问题吗?



是的。它调用未定义的行为。


>
问候

char * func()
{

char c;



c是一个自动变量。它出现在

这个代码块的开头。


return(& c);



当函数返回时它就不存在了。


>}
#include< stdio.h>
int main()

char * ch;

ch = func();



此时ch接收刚刚退出

存在的c的地址。 (根据定义,ch中的值变得不确定。)


* ch =''A'';



在这里,您尝试将值A存储到不再存在
的对象中。 (从技术上讲,您正在尝试评估一个不确定的

值,该值会调用未定义的行为。)


pritnf("%c",* CH);



此语句还通过尝试评估

ch中的地址来调用未定义的行为。


>}



-

删除电子邮件的del


Hi,
Is there any problem with the following code?

Regards

char *func()
{
char c;
return (&c);
}
#include<stdio.h>
int main()
{
char *ch;
ch = func();
*ch = ''A'';
pritnf("%c",*ch);
}

解决方案

On Aug 25, 11:22 am, ramu <ramu....@gmail.comwrote:

Hi,
Is there any problem with the following code?

Regards

char *func()
{
char c;
return (&c);}

#include<stdio.h>
int main()
{
char *ch;
ch = func();
*ch = ''A'';
pritnf("%c",*ch);

}

pritnf("%c",*ch);

>Sorry. It should be printf("%c", *ch);


On Aug 24, 11:22 pm, ramu <ramu....@gmail.comwrote:

Hi,
Is there any problem with the following code?

Regards

char *func()
{
char c;
return (&c);}

#include<stdio.h>
int main()
{
char *ch;
ch = func();
*ch = ''A'';
pritnf("%c",*ch);

}

Looks like homework. How about if you explain whether *you* think
there''s a problem, and why or why not. And don''t forget, when you
submit the assignment, to acknowledge and cite as references the posts
of those who replied to you.


On Sun, 24 Aug 2008 23:22:01 -0700 (PDT), ramu <ra******@gmail.com>
wrote:

>Hi,
Is there any problem with the following code?

Yes. It invokes undefined behavior.

>
Regards

char *func()
{
char c;

c is an automatic variable. It comes into existence at the start of
this block of code.

return (&c);

And it goes out of existence when the function returns.

>}
#include<stdio.h>
int main()
{
char *ch;
ch = func();

At this point ch receives the address of c which has just gone out of
existence. (By definition, the value in ch becomes indeterminate.)

*ch = ''A'';

Here you attempt to store the value ''A'' into an object that no longer
exists. (Technically, you are trying to evaluate an indeterminate
value which invokes undefined behavior.)

pritnf("%c",*ch);

This statement also invokes undefined behavior by trying to evaluate
the address in ch.

>}

--
Remove del for email


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

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