Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

caret in mvprintw but only in loop

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading