gets_s函数的实现, [英] Implementation of gets_s function,

查看:1580
本文介绍了gets_s函数的实现,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有gets_s函数的这个问题。这是我的代码片段:

  char  * string; 
int n;
printf( 输入最大长度:\ n);
scanf_s( %d,& n);
printf( 输入字符串:\ n);
gets_s(string);
printf( %s,string);



我遇到的错误是:

错误C2660:''gets_s'':函数不带1个参数

IntelliSense:没有重载函数的实例gets_s匹配参数列表参数类型是:(char *)




但是,如果我静态分配

char string [ARRAY]; (将宏定义为ARRAY)我没有遇到任何问题。我希望最终用户定义自己的大小。有人请帮帮我!

解决方案

gets_s不带一个参数 - 需要两个: http://en.cppreference.com/w/c/io/gets [ ^ ]你需要提供一个大小以及一个缓冲区。



但是,由于您显示的代码没有为字符串参数分配任何内存,您只需传入一个空指针,您的应用程序将立即崩溃! :笑:



尝试使用malloc函数分配一些空间 - 但是当你完成它时别忘了释放它!


gets()vs gets_s()



gets_s()是新C11标准的一部分并取代旧的获取(),但更安全。你可以使用gets()但是你会收到一个警告,鼓励你切换到gets_s()。与没有执行边界检查的gets()相反,gets_s()限制长度



是什么让gets_s()更安全?

gets_s()要求你添加另一个参数,它是预期输入的最大大小,以防止因无限字符串而导致的访问冲突(最后没有''null'),

gets()和gets_s()之间的区别在于gets()将字符写入您提供的数组并信任您有足够的空间,而gets_s()在检查长度之前写作。对于以下内容:



  char  * array1 =  new   char  [ 20 ]; 
gets_s(array1); / * 错误* /

char array2 [ 20 ];
gets_s(array2);





编译器可以确定(在编译时)array2的大小为20,但是不要对array1做同样的事情。每当你传递一个指向gets_s()的指针时,你还需要传递它的代码来编译。



请参阅Microsoft文档其他 [ ^ ]。


I have this problem with the gets_s function. This is my code snippet:

char *string;
	int n;
	printf("Enter the max length:\n");
	scanf_s("%d",&n);
	printf("Enter the string:\n");
	gets_s(string);
	printf("%s",string);


The Errors I''ve been facing is this:
error C2660:''gets_s'':function does not take 1 arguments
IntelliSense: no instance of overloaded function "gets_s" matches the argument list argument types are: (char *)


However, if I statically allocate
char string[ARRAY];(with a macro defined for size as ARRAY) I havent been facing any problem. I want the end user to define its own size. Someone please help me out!

解决方案

gets_s does not take a single parameter - it takes two: http://en.cppreference.com/w/c/io/gets[^] You need to supply a size as well as a buffer.

However, since the code you show does not allocate any memory to the string parameter all you will do is pass in a null pointer and your application will immediately crash! :laugh:

Try using the malloc function to allocate some space - but don''t forget to free it when you are finished with it!


gets() vs gets_s()

gets_s() is part of the new C11 standard and the replacement of the old gets(), but is safer. You can use gets() but you will then get a warning encouraging you to switch to gets_s(). As opposed to gets() that doesn''t perform bounds checking, gets_s() restrict the length

What makes gets_s() safer?
gets_s() requires you to add another parameter which is the maximum size of the expected input to prevent access violation due to endless strings (without ''null'' in their end),


The difference between gets() and gets_s() is that gets() writes characters out to an array that you provide and trusts that you have enough room, while gets_s() does a check for length before writing. For the following:

char* array1 = new char[20];
gets_s(array1);  /* error */

char array2[20];
gets_s(array2);



The compiler can determine (at compile time) that array2 is of size 20, but can not do the same for array1. Whenever you pass a pointer to gets_s(), you also need to pass its length for the code to compile.

See the Microsoft documentation here[^].


这篇关于gets_s函数的实现,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆