[英]How to enter char* in C?
Its a simple input that I want to make for char*. 我想为char *输入一个简单的输入。 Why is this not working?
为什么这不起作用? It throws me an exception that I can't resolve..
它引发了我无法解决的异常。
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
char* GetCharSeq()
{
char *s = (char*)malloc(100);
scanf_s("%s", s);
return s;
}
int main()
{
char* charseq;
charseq = GetCharSeq();
return 0;
}
You have undefined behavior in your code. 您的代码中有未定义的行为 。 You have it because you provide to few arguments to the
scanf_s
function. 之所以拥有它,是因为您为
scanf_s
函数提供了很少的参数。
For every string argument, you need to provide not only the destination string but also the size of the string. 对于每个字符串参数,您不仅需要提供目标字符串,还需要提供字符串的大小。 So change your call to
因此,将通话更改为
scanf_s("%s", s, 100);
modify your code 修改您的代码
char* GetCharSeq()
{
char *s = (char*)malloc(100);
gets(s);
return s;
}
This will work. 这将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.