一个非常简单的问题 [英] A very simple question

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

问题描述



为什么下面的代码会给我一个核心转储?


#include< stdio.h>


int main(){

char * a =" Marmagya" ;;


printf("声明为:%s" ;,* a);


返回0;

}

问候

Marmagya

解决方案

Marmagya< ma ****** @ epatra.com_nospam>潦草地写道:


为什么以下代码会给我一个核心转储?
#include< stdio.h>
int main(){
char * a =" Marmagya" ;;
printf("声明是:%s",* a);


%s需要一个指针,然后开始读取该指针

指向的位置,直到它达到0字节为止。你给它一个字母''M''

代替。这使得%s将''M​​'视为指针,并尝试读取指向它的
。但是哪个地址M转换为未定义

行为,而且很可能该地址甚至不属于你的b $ b。所以你得到一个核心转储。

通过给%s提供它想要的东西来修复问题:一个指针。


printf("声明是:% s",a);

返回0;
}




-

/ - - Joona Palaste(pa*****@cc.helsinki.fi)--------------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

你会得到瘟疫。

- 蒙哥马利伯恩斯


Marmagya写道:

为什么下面的代码会给我一个核心转储?

#include< stdio.h>

int main(){
char * a =" Marmagya" ;;
printf("声明是:%s",* a);
返回0;
}




没有应该关于它;它是未定义的行为,所以

实现可以做它喜欢的事情,包括忽略它,打印你​​的字符串,打印不同的字符串,或画你的脚趾甲

紫色。幸运的是,你的实现属于核心转储。


%s想要一个字符串。 * a是一个字符(即M)。今天BOOM。


也许你的意思是'a`。


[难道你的编译器不会警告你这行吗?如果

适当激发,海湾合作委员会将会这样做。]


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


在< bf ********** @ oravannahka.helsinki.fi> Joona I Palaste< pa ***** @ cc.helsinki.fi>写道:

Marmagya< ma ****** @ epatra.com_nospam>潦草地写道:


为什么下面的代码会给我一个核心转储?


#include< stdio .h>


int main(){
char * a =" Marmagya" ;;


printf("语句是:%s",* a);



通过给%s提供它想要的东西来修复问题:指针。

printf("声明是:%s",a);

return 0;
}




有线索的人,甚至一半的人,也提供所需的换行符

字符:


printf("声明是:%s \ n",a);


Dan

-

Dan Pop
DESY Zeuthen,RZ集团

电子邮件: Da ***** @ ifh.de

Hi,
Why should the following code give me a core dump?

#include <stdio.h>

int main(){
char *a="Marmagya";

printf("The statement is : %s", *a);

return 0;
}
Regards
Marmagya

解决方案

Marmagya <ma******@epatra.com_nospam> scribbled the following:

Hi,
Why should the following code give me a core dump? #include <stdio.h> int main(){
char *a="Marmagya"; printf("The statement is : %s", *a);
%s expects a pointer, and then starts reading from where that pointer
points onwards until it hits a 0 byte. You''re giving it a char ''M''
instead. This makes %s treat the ''M'' as a pointer, and tries to read
where it points to. But which address ''M'' translates to is undefined
behaviour, and it is most likely that address doesn''t even belong to
you. So you get a core dump.
Fix the problem by giving %s what it wants: a pointer.

printf("The statement is : %s", a);
return 0;
}



--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You will be given the plague."
- Montgomery Burns


Marmagya wrote:

Why should the following code give me a core dump?

#include <stdio.h>

int main(){
char *a="Marmagya";
printf("The statement is : %s", *a);
return 0;
}



There''s no "should" about it; it''s undefined behaviour, so the
implementation can do what it likes, including ignoring it, printing
your string, printing a different string, or painting your toenails
purple. Luckily, your implementation falls into a core dump.

%s wants a string. *a is a character (viz, ''M''). BOOM today.

Perhaps you meant just `a`.

[Doesn''t your compiler warn you about this line? GCC will, if
suitably provoked.]

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


In <bf**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:

Marmagya <ma******@epatra.com_nospam> scribbled the following:

Hi,
Why should the following code give me a core dump?


#include <stdio.h>


int main(){
char *a="Marmagya";


printf("The statement is : %s", *a);



Fix the problem by giving %s what it wants: a pointer.

printf("The statement is : %s", a);

return 0;
}



People with a clue, or even half of one, also provide the required newline
character:

printf("The statement is : %s\n", a);

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de


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

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