是否有可能使用不知道在C数组的长度变? [英] Is it possible using gets without knowing the length of the array in c?

查看:161
本文介绍了是否有可能使用不知道在C数组的长度变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们想用被在C我们会做这样的事情:

If we want to use gets in c we will do something like:

int main(void) {
  char str[100];
  while (gets(str)) {
  printf("%s\n",str); 
 }
}

我们必须知道海峡第一的长度(即100),然后使用得到。可以使用它不知道在C数组的长度变?

We have to know the length of str first(which is 100) and then use gets. Is it possible using gets without knowing the length of the array in c?

推荐答案

如果你的意思是使用获得安全,不,这是不可能的。

If you mean using gets safely, no, it's not possible.

文章:不使用获得,因为不知道第一的长度,它可能会导致缓冲区溢出。使用 与fgets 代替,或使用 gets_s 的C11。

Advice: don't use gets, because without knowing the length first, it may cause buffer overflow. Use fgets instead, or use gets_s in C11.

其实,已经从 stdio.h中自C11去除。 (在C99,这是德precated)

In fact, gets has been removed from stdio.h since C11. (In C99, it's deprecated)

这篇关于是否有可能使用不知道在C数组的长度变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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