Home > Mobile >  caret in mvprintw but only in loop
caret in mvprintw but only in loop

Time:12-06

I have the following program

#include <ncurses.h>

int main() {
    initscr();
    const char c = static_cast<char>(65);
    mvprintw(0, 0, "%s", &c);
    getch();
    endwin();
    return 0;
}

it prints simple "A" and waits.

enter image description here

If I modifying "x" argument of mvprintw

        mvprintw(0, 1, "%s", &c);

it will print "A" with empty space prepend on beginning.

enter image description here

If I will add for loop starting from 0 it also works as expected

int main() {
    initscr();
    for (int i = 0; i < 1; i  ) {
        const char c = static_cast<char>(65);
        mvprintw(0, i, "%s", &c);
    }
    getch();
    endwin();
    return 0;
}

will show the same result as in first example.

But if this loop starts from 1 there is stragne ^A at the end.

This code:

int main() {
    initscr();
    for (int i = 1; i < 2; i  ) {
        const char c = static_cast<char>(65);
        mvprintw(0, i, "%s", &c);
    }
    getch();
    endwin();
    return 0;
}

produces

enter image description here

and I even do not know how to debug it?

CodePudding user response:

If mvprintw is anything like printf then you are supposed to provide a nul terminated string when you use %s. Try this instead

const char c[] = { static_cast<char>(65), '\0' }; 
mvprintw(0, i, "%s", c); 

Note in this version c not &c.

  • Related