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

How to check what value is being returned by gets() function in C?

我正在尝试在 C 中打印 gets() 函数 returns。

我尝试了各种格式说明符,但 none 似乎有所帮助。

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 或更早版本)returns 其参数或 NULL

注意是 marked obsolescent in C99 (2007 TC) and 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]);