一个非常简单的问题

编程入门 行业动态 更新时间:2024-10-10 23:18:44
本文介绍了一个非常简单的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

解决方案

Marmagya< ma ****** @ epatra_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 +++ | | 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常见问题解答: www.faqs/faqs/by-newsgrou...mp.lang.c.html C欢迎: www.angelfire/ms3/bchambl...me_to_clc.html

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

Marmagya< ma ****** @ epatra_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_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+++| | 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: www.faqs/faqs/by-newsgrou...mp.lang.c.html C welcome: www.angelfire/ms3/bchambl...me_to_clc.html

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

Marmagya <ma******@epatra_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

更多推荐

一个非常简单的问题

本文发布于:2023-11-10 07:12:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!