首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于gets的用法解决思路

2012-09-07 
关于gets的用法C/C++ code#include iostream#include stringusing namespace stdint main(){char str

关于gets的用法

C/C++ code
#include <iostream>#include <string>using namespace std;int main(){    char str[5];    gets(str);    cout << str;    return 0;}


输入:1234567
已经超过5个字符,为什么不溢出呢?

[解决办法]
这就是 gets的危害。永远不要用gets。 最高级危险函数。
[解决办法]
实际上都已经发生溢出了:
C/C++ code
#include <iostream>#include <string>using namespace std;int main(){    char str1[5] = {0};    char str[5] = {0};    char buf[10]= {0};    gets(str);    cout << "str: " <<str <<endl;    cout << "str1:" <<str1 <<endl;    cout << "buf :" <<buf <<endl;    return 0;} 

热点排行