ncurses inchstr 函数没有按预期工作?

ncurses inchstr function not working as intended?

#include <curses.h>
#include <fstream>

int main(){

    initscr();
    mvwprintw(stdscr, 1,1, "hello world");
    wmove(stdscr, 1,1);

    chtype* p = 0;
    int n = 0;
    n = winchstr(stdscr, p);

    std::ofstream test("test.txt");

    test << n << " " << (p == nullptr);

    endwin();

}

在上面我打印hello world,将光标移回句子的开头,并尝试使用[=13=将屏幕上的内容存储到p ].除了在文件 test.txt 中,我只看到表明 p 的输出 -1 1 没有改变,并且返回了 ERR (即 -1),如 documentation 所述:

int winchstr(WINDOW *win, chtype *ch);

Description: These routines read a chtype or cchar_t string from the window, starting at the current or specified position, and ending at the right margin, or after n elements, whichever is less.

Return Value: All functions return the number of elements read, or ERR on error.

我是不是误解了文档?从hello world 开始的行的内容在使用winchstr 之后应该存储在p 指向的位置,不是吗?为什么返回错误?

位置p是一个空指针; ncurses 可能会看到,并拒绝使用它。手册页说

No error conditions are defined. If the chstr parameter is null, no data is returned, and the return value is zero.

如果您使用 winchstr 和非空指针,ncurses 将(必须)假定您为结果提供了足够的 space。使用 winchnstr 指定大小。