[英]Scanf with string format
I made a code that supposed to take stdin and make sure it is true depending on my scanf format. 我编写了一个代码,该代码应该采用stdin并根据我的scanf格式确保它是正确的。
Here is the code 这是代码
#include <stdio.h>
int main()
{
int i;
char* a;
i = 0;
while (1 == scanf("/%[^/ \t\n]", a[i++]))
printf(">%s<\n", a[i-1]);
return 0;
}
sample input: 样本输入:
/test
sample output: 样本输出:
/test
Segmentation fault
I have no idea what is causing the segmentation fault 我不知道是什么原因造成细分错误
You need to provide the address of the char *
, to the scanf
and printf
function, but you are providing an element of the same. 您需要向scanf
和printf
函数提供char *
的地址,但是要提供相同的元素。 Also, you need to allocate memory to char *a
. 另外,您需要将内存分配给char *a
。
char *a = malloc(15*sizeof(char));
while (1 == scanf("/%[^/ \t\n]", a))
printf(">%s<\n", a);
Output : 输出:
>test<
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.