scanf 转换为指针
我有这样的代码:
char *ps = "hello";
scanf("%s", ps);
我想写一个字符串,让我们假装“a”,到指针而不使用数组。尝试将字符串扫描到 ps 中会导致错误被信号 10 中断:SIGBUS。我很高兴听到对这种行为的解释。
I have this code:
char *ps = "hello";
scanf("%s", ps);
I want to write a string, let's pretend "a", to pointer without using an array. Trying to scanf a string into ps causes error interrupted by signal 10: SIGBUS. I'd be happy to hear an explanation for such a behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
指针指向字符串文字
因此此调用
尝试更改字符串文字。
您不能更改字符串文字。任何更改字符串文字的尝试都会导致未定义的行为。
来自 C 标准(6.4.5 字符串文字)
The pointer points to a string literal
So this call
tries to change a string literal.
You may not change string literals. Any attempt to change a string literal results in undefined behavior.
From the C Standard (6.4.5 String literals)