C语言 使用带有指向字符的指针的 scanf 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14546018/
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
using scanf function with pointers to character
提问by Saurabh.V
I have written the following piece of code.
我写了下面的一段代码。
int main(){
char arrays[12];
char *pointers;
scanf("%s",arrays);
scanf("%s",pointers);
printf("%s",arrays);
printf("%s",pointers);
return 0;
}
Why does it give an error when I write `scanf("%s",pointers)?
为什么我写 `scanf("%s",pointers) 时会报错?
回答by Junior Fasco
char *pointers;
must be initialized.You can not scan string into pointersuntil you point it to some address. The computer needs to know where to store the value it reads from key board.
必须初始化。在将字符串pointers指向某个地址之前,您无法将其扫描到。计算机需要知道在哪里存储从键盘读取的值。
int main(){
char arrays[12];
char *pointers= arrays;
scanf("%s",pointers);
printf("%s",pointers);
return 0;
}
回答by Gigi
Because you're writing to an address in memory that has not been initialized. Writing to memory pointer by an uninitialized pointer invokes undefined behaviour. Either allocate enough memory:
因为您正在写入内存中尚未初始化的地址。通过未初始化的指针写入内存指针会调用未定义的行为。要么分配足够的内存:
pointers = malloc(256);
if(!pointers)
perror("malloc");
else
scanf("%255s", pointers);
Or declare it as a static array:
或者将其声明为静态数组:
char pointers[256];
You should also consider using fgets()instead of scanf().
您还应该考虑使用fgets()而不是 scanf()。
You may want to read i you are interested in fgets():
您可能想阅读我对 fgets() 感兴趣的内容:
回答by ATOzTOA
char *pointers;creates a pointer variable.pointersis theaddresspointed to bypointers, which is indeterminate by default.*pointersis the data in the address pointed to bypointers, which you cannot do until address is assigned.
char *pointers;创建一个指针变量。pointers是address指向的pointers,默认情况下是不确定的。*pointers是指向的地址中的数据,在pointers分配地址之前您无法执行此操作。
Just do this.
就这样做。
char arrays[12];
char *pointers;
pointers = arrays;
scanf("%s",pointers);
回答by autistic
pointersis being used without initialisation, like int x; printf("%d\n", x);. You need to make your pointer point to something before using it. Which book are you reading?
pointers在没有初始化的情况下使用,例如int x; printf("%d\n", x);. 在使用它之前,你需要让你的指针指向某个东西。你在读哪本书?
回答by thepratt
Could you elaborate on the error, i'm not around a compiler right now.
你能详细说明这个错误吗,我现在不在编译器周围。
But for scanf and printf to work you must have this at the top of your program:
但是要使 scanf 和 printf 工作,您必须在程序的顶部放置它:
#include <stdio.h>
#include <stdlib.h>
Both are standard libraries for C. IO contains scanf, I'm fairly sure printf is in the same. But until you know which libraries you need for which functions it doesn't hurt to include both standard libraries for every program. Try to use custom header files as well so you don't need mass #includes for every file.
两者都是 C 的标准库。IO 包含 scanf,我很确定 printf 是相同的。但是,除非您知道哪些函数需要哪些库,否则为每个程序都包含两个标准库并没有什么坏处。也尝试使用自定义头文件,这样您就不需要为每个文件使用大量 #includes。
Don't forget mallocstatements for memory allocation.
不要忘记malloc内存分配语句。
But I'm unsure what you're attempting to do with your code, please elaborate?
但我不确定你想用你的代码做什么,请详细说明?
回答by md5
pointersis an unitialized pointer. You are not able to write into it. You shall allocate enough memory to store a string, as you did with arrays. With a pointer, it is possible to use dynamic allocation (cf. malloc).
pointers是一个未初始化的指针。您无法写入其中。你应该分配足够的内存来存储一个字符串,就像你对arrays. 使用指针,可以使用动态分配(参见malloc)。

