C语言 Char* p 和 scanf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4900394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Char* p, and scanf
提问by Fingolfin
I have been trying to look for a reason why the following code is failing, and I couldn't find one. So please, excuse my ignorance and let me know what's happening here.
我一直试图寻找以下代码失败的原因,但我找不到。所以请原谅我的无知,让我知道这里发生了什么。
#include<stdio.h>
int main(void){
char* p="Hi, this is not going to work";
scanf("%s",p);
return 0;
}
As far as I understood, I created a pointer p to a contiguous area in the memory of the size 29 + 1(for the \0). Why can't I use scanf to change the contents of that?
据我所知,我创建了一个指针 p 指向内存中大小为 29 + 1(对于 \0)的连续区域。为什么我不能使用 scanf 来更改它的内容?
P.S Please correct me If I said something wrong about char*.
PS请纠正我如果我对char *说错了。
回答by
char* p="Hi, this is not going to work";
this does not allocate memory for you to write
这不会为您分配内存来写入
this creates a String Literalwhich results inUndefined Behaviourevery time you try to change its contents.
这将创建一个String Literal结果,Undefined Behaviour每次您尝试更改其内容时。
to use pas a buffer for your scanfdo something like
char * p = malloc(sizeof(char) * 128); // 128 is an Example
使用p作为缓冲为您scanf做这样的事情
char * p = malloc(sizeof(char) * 128); // 128 is an Example
OR
或者
you could as well do:
你也可以这样做:
char p[]="Hi, this is not going to work";
Which I guess is what you really wanted to do.
我想这就是你真正想做的。
Keep in mind that this can still end up being UBbecause scanf()does not check whether the place you are using is indeed valid writable memory.
请记住,这仍然可能最终成为UB因为scanf()不检查您正在使用的地方是否确实是有效的可写内存。
remember :
记住 :
char * pis a String Literal and should not be modified
char * p是字符串文字,不应修改
char p[] = "..."allocates enough memory to hold the String inside the "..."and may be changed (its contents I mean).
char p[] = "..."分配足够的内存来保存内部的字符串"..."并且可能会更改(我的意思是它的内容)。
Edit :
编辑 :
A nice trick to avoid UBis
避免的一个好技巧UB是
char * p = malloc(sizeof(char) * 128);
scanf("%126s",s);
回答by Péter T?r?k
ppoints to a constant literal, which may in fact reside in a read-only memory area (implementation dependent). At any rate, trying to overwrite that is undefined behaviour. I.e. it might result in nothing, or an immediate crash, or a hidden memory corruption which causes mysterious problems much later. Don't ever do that.
p指向一个常量文字,它实际上可能驻留在只读内存区域(取决于实现)。无论如何,试图覆盖那是未定义的行为。即它可能导致什么也没有,或者立即崩溃,或者隐藏的内存损坏,这在很久以后会导致神秘的问题。永远不要那样做。
回答by user210504
It is crashing because memory has not been allocated for p. Allocate memory for p and it should be ok. What you have is a constant memory area pointing to by p. When you attempt to write something in this data segment, the runtime environment will raise a trap which will lead to a crash.
它正在崩溃,因为尚未为 p 分配内存。为 p 分配内存应该没问题。你拥有的是一个由 p 指向的常量内存区域。当您尝试在此数据段中写入某些内容时,运行时环境将引发会导致崩溃的陷阱。
Hope this answers your question
希望这能回答你的问题
回答by Jonathan Wood
scanf()parses data entered from stdin (normally, the keyboard). I think you want sscanf().
scanf()解析从标准输入(通常是键盘)输入的数据。我想你想要sscanf()。
However, the purpose of scanf()is to part a string with predefined escape sequences, which your test string doesn't have. So that makes it a little unclear exactly what you are trying to do.
但是,目的scanf()是将字符串与预定义的转义序列分开,而您的测试字符串没有。所以这让你有点不清楚你想要做什么。
Note that sscanf()takes an additional argument as the first argument, which specifies the string being parsed.
请注意,sscanf()将附加参数作为第一个参数,它指定要解析的字符串。

