LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2004, 02:47 AM   #1
frenchi
Member
 
Registered: Dec 2003
Location: Rennes - FRANCE
Distribution: Slackware 9.1
Posts: 36

Rep: Reputation: 15
Exclamation getch() or noecho() -> segmentation fault !


Hi there !
I've got a problem, I just tried to detect a key has been pressed, but it doesn't seem to work, little explanation :
Code:
#include <curses.h> /* And other ;)*/
int main(void)
{
int return_echo;

return_echo=noecho();/* Even if there is this only instruction*/
printf("%d", return_echo);/*optionnal*/

return 0;
}
When I compile it with Wall and everything (gcc), I haven't got any error.
If I try it, I've got a "segmentation fault", noecho() or getch() does the same thing...
I'm disappointed, what can I do

Thanks in advance !
 
Old 07-23-2004, 05:42 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
noecho() is a function from the ncurses library. And you cannot use ncurses-functions until you initialize the ncurses screen with initscr(). When you're program is done with the ncurses-screen you need dispose if off with endwin(). While ncurses screen is active (i.e. code between initscr() and endwin() ) printf()'s will not work. So you need to do the printf() after closing the ncurses screen with endwin().

Fixed program:
Code:
#include <curses.h>

int main(void)
{
	int return_echo;

	initscr();
	return_echo=noecho();
	endwin();

	printf("%d\n", return_echo);  /* note: After endwin() */
	return 0;
}
( compile with "gcc -Wall -lncurses -o progname progname.c" )

Last edited by Hko; 07-23-2004 at 05:44 AM.
 
Old 07-23-2004, 10:50 PM   #3
psiakr3w
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Rep: Reputation: 15
I actually ran into the same problem when I tried out something that was suggested in another thread. But then I searched google for 'ncurses' and came up with the following:

http://en.tldp.org/HOWTO/NCURSES-Programming-HOWTO/

which got me to try its Hello World program (chapter 2) and all was well again Check it out.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
segmentation fault samprabhu Linux - General 3 07-29-2005 07:17 PM
Segmentation fault, WHY?? eXor Slackware 8 07-20-2005 01:55 PM
segmentation fault pippet Programming 4 01-24-2005 01:02 AM
Segmentation fault sin-x Slackware 2 01-12-2005 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration