Search code examples
cncurses

ncurses keeps giving SIGSEGV


I'm a bit new to C and ncurses, and I experiment with what I learn to make sure I understand it. I'm learning ncurses from here, and C from here, but I chose to not learn GTK from that C book because I'm not interested in front end development. I was trying to experiment with the mvprintw function in ncurses, but it didn't work. Running it in gdb shows this:

This program will print a letter on whatever coordinates you want
Enter an x coordinate: 2
Enter a y coordinate: 3
Enter the letter to print: 
                           Program received signal SIGSEGV, Segmentation fault.
                                                                               strlen () at ../sysdeps/arm/armv6/strlen.S:26
26  ../sysdeps/arm/armv6/strlen.S: No such file or directory.
(gdb) quit
A debugging session is active.

    Inferior 1 [process 7659] will be killed.

Quit anyway? (y or n) n
Not confirmed.
(gdb) c
Continuing.

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb)

From what I understand, SIGSEGV means that I accessed a restricted part of the memory. How did I do this, and how do I prevent this? Here's my code:

#include <stdio.h>
#include <ncurses.h>

int main(int argc, char *argv[]){
  // initialize ncurses
  initscr();
  // stop echoing
  noecho();
  printw("This program will print a letter on whatever coordinates you want\nEnter an x coordinate: ");
  refresh();
  // set 'x' to the user input
  char x = getch();
  printw("%c\n", x);
  refresh();
  printw("Enter a y coordinate: ");
  refresh();
  // set 'y' to the user input
  char y = getch();
  printw("%c\n", y);
  refresh();
  printw("Enter the letter to print: ");
  // set input as letter
  char word = getch();
  printw("%s", word);
  refresh();
  // mvprintw(y, x, "%s", word);
  // refresh();
  // getch();
  // endwin();

  return 0;
}

(I have uncommented the last few lines since that's not where the error occurs)


Solution

  •   char word = getch();
      printw("%s", word);
    

    is wrong. As you did in the lines before that, you should use %c, not %s, for printing one character.