c - 如何检查 C 中的 gets() 函数返回什么值?

标签 c gets

我正在尝试打印 gets() 函数在 C 中返回的内容。

我尝试了各种格式说明符,但似乎没有任何帮助。

char a[100];
char (*p)[100];
p=gets(a);
printf("%s",p);

它只是说段错误。

最佳答案

gets() function

The gets function returns [the original argument] if successful. If end-of-file is encountered and no characters have been read into the array, [...] a null pointer is returned. If a read error occurs during the operation, [...] a null pointer is returned.

因此,gets() 函数(C99 或更早版本)返回其参数或 NULL

请注意,它是 marked obsolescent in C99 (2007 TC)removed from the Standard in C11 .

<小时/>
char a[100];
char *p;
p = gets(a); // assign a (&a[0]) to p if no errors
printf("%c is the same as %c\n", a[0], p[0]);

关于c - 如何检查 C 中的 gets() 函数返回什么值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344310/

相关文章:

c - 在哪里可以找到 ncurses C API 的完整引用?

c - 11线程程序段错误

c - strtold 解析的数字小于给定数字

C - scanf() 与 gets() 与 fgets()

c - 在C中读取进程内存

c - 此代码正在生成段核心转储

c - 调整 gets() 以避免缓冲区溢出

c - c语言中的gets函数有问题

c - 为什么在 C 代码中,当除了其中的字母没有发生任何变化时,get 和 put 的行为会有所不同?

c - gets 和 put 获取并打印字符串