c - 使用 readline() 函数时,为什么必须将 NULL 作为参数传递?

标签 c xcode null char readline

在 C 编程中,为什么在使用 readline() 函数时将 NULL 作为参数传递? 除了使用 const char 之外,还有其他存储名称的方法吗?

#import <readline/readline.h>
#import <stdio.h>

int main(int argc, const char * argv[])
{
    printf("Who is cool? ");
    const char *name = readline(NULL);
    printf("%s is cool!\n\n",name);

    return 0;
}

编辑:当使用这段代码(在 Xcode 中)时,我注意到我输入的字符被回显了。 所以我从控制台看到的实际上是这样的:

who is cool? MMiikkeeyy

Mikey is cool!

是否有解释为什么会发生这种情况?

最佳答案

readline 函数是 a GNU thing ,允许用户使用各种 shell 中的所有编辑功能进行输入。

参数只是在要求输入之前发出提示,因此您的代码等效于(在 C 代码中,因为我的 Mac 不方便):

#include <stdio.h>
#include <readline/readline.h>

int main (int argc, const char * argv[]) {
    const char *name = readline ("Who is cool? "); // Don't need separate printf.
    printf ("%s is cool!\n\n", name);

    return 0;
}

可以找到有关如何使用它的更多详细信息 here ,第一个句子中的链接引用的页面之一。

关于c - 使用 readline() 函数时,为什么必须将 NULL 作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27614772/

相关文章:

c - 使用 s 但 gettng 段错误,当我将 `s` 替换为 `s1

c - 尝试 printf 进度指示器时 c 循环中的奇怪怪癖

ios - Xcode 5 的新文档解析是如何工作的?

iphone - 使用 Facebook SDK 和 Windows Azure SDK 时的 xcode 链接问题

c++ - 初始化指针 vector (自动)

c - GetProcAddress 返回 NULL

ios - Swift - 函数在附加字典中的键值后仅返回 nil?

objective-c - oc中_xxx和self->_xxx有什么区别

c - 获取一个与内核中的任何内容都不相关的唯一文件描述符

ios - Xcode 从 iOS 模拟器中的 App 恢复代码