Ncurses: temporarily leaving curses mode not working

Discussion in 'C Programming' started by dachteruit, Nov 11, 2010.

  1. dachteruit

    dachteruit Guest

    Hi all,

    First of all I'm a newbie in C programming and I was playing around
    with ncurses.
    I've followed the instructions as described in
    http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/misc.html#TEMPLEAVE to
    leave curses mode temporarily. So I wrote the following:

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

    int main(){
    int i=0, j=2560;
    char last, result[j];
    initscr();
    cbreak();
    refresh();
    do {
    last = getch();
    def_prog_mode();
    endwin();

    strcat(last, result);
    printf("Last one is: %c\n", last); //doesn't work
    printw("The very last one is: %c\n", last); //printw does still
    work!!!

    reset_prog_mode();
    refresh();
    }while(last != 's');

    for(j=0;j<2560;j++) printw("%c", result[j]);
    refresh();
    getch();

    endwin();

    return 0;
    }

    Can anyone explain me why printw works and printf don't when I'm
    temporarily out of curses mode?
    In addition you may explain too why the for-loop returns a scrambled
    output, but that's not yet my main problem.

    Thanks,

    Nico
     
    dachteruit, Nov 11, 2010
    #1
    1. Advertising

  2. dachteruit <> writes:
    > First of all I'm a newbie in C programming and I was playing around
    > with ncurses.
    > I've followed the instructions as described in
    > http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/misc.html#TEMPLEAVE to
    > leave curses mode temporarily. So I wrote the following:


    ncurses isn't part of the C language/library standard.
    comp.unix.programmer is probably a better place to ask about it.

    I'll comment on some purely C-related aspects of your code.

    > #include <stdio.h>
    > #include <ncurses.h>
    > #include <string.h>
    >
    > int main(){


    "int main(void)" is better. ("int main()" would be preferred in C++.)

    > int i=0, j=2560;
    > char last, result[j];


    This declares last and results as variable-length arrays (VLAs), which
    means that the length of the array can be determined at run time. Since
    the values you're using are always going to be the same, there's not
    much point in using VLAs rather than just plain fixed-length arrays.

    Note that the length of a VLA is fixed at run time when the object is
    created; changing the value of i won't change the size of last.

    For "result", for example, you could have written:

    #define RESULT_LENGTH 2560;
    int j = RESULT_LENGTH;
    char result[RESULT_LENGTH];

    But since you assign a new value to j (in the for loop) before ever
    reading it, there's no point in initializing it here.

    As for last, since i is 0, you're trying to create a zero-length
    array. C doesn't support this, and your program's behavior is
    undefined. (The most likely result is that attempting to access
    any elements of last will scribble on other memory.)

    Hmm. You never change the value of i, so it's always 0. last would
    be a char object if it existed. Probably you just wanted to declare
    last as a char.

    Correction: since you're it to store the value returned by getch(),
    it should be an int (consult your documentation, you'll find that
    getch(), at least the one provided by ncurses, returns an int).

    > initscr();
    > cbreak();
    > refresh();
    > do {
    > last = getch();
    > def_prog_mode();
    > endwin();
    >
    > strcat(last, result);
    > printf("Last one is: %c\n", last); //doesn't work
    > printw("The very last one is: %c\n", last); //printw does still
    > work!!!
    >
    > reset_prog_mode();
    > refresh();
    > }while(last != 's');
    >
    > for(j=0;j<2560;j++) printw("%c", result[j]);


    And since you're using C99-specific features anyway (VLAs), you might
    as well declare j in the loop:

    for (int j = 0; j < RESULT_LENGTH; j ++) ...

    Note that I'm using a named constant, which makes maintenance of
    your program easier; if you want to make the result array bigger
    or smaller, you only have to change the number in one place.

    > refresh();
    > getch();
    >
    > endwin();
    >
    > return 0;
    > }

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 11, 2010
    #2
    1. Advertising

  3. dachteruit

    Ian Collins Guest

    On 11/12/10 08:23 AM, Keith Thompson wrote:
    > dachteruit<> writes:
    >> First of all I'm a newbie in C programming and I was playing around
    >> with ncurses.
    >> I've followed the instructions as described in
    >> http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/misc.html#TEMPLEAVE to
    >> leave curses mode temporarily. So I wrote the following:
    >> #include<stdio.h>
    >> #include<ncurses.h>
    >> #include<string.h>
    >>
    >> int main(){

    >
    > "int main(void)" is better. ("int main()" would be preferred in C++.)
    >
    >> int i=0, j=2560;
    >> char last, result[j];

    >
    > This declares last and results as variable-length arrays (VLAs), which
    > means that the length of the array can be determined at run time. Since
    > the values you're using are always going to be the same, there's not
    > much point in using VLAs rather than just plain fixed-length arrays.


    Also note that a VLA with a length of zero isn't going to do you much
    good and is probably the cause of many of your problems!

    --
    Ian Collins
     
    Ian Collins, Nov 11, 2010
    #3
  4. dachteruit

    Alan Curry Guest

    In article <>,
    dachteruit <> wrote:
    >int main(){
    > int i=0, j=2560;
    > char last, result[j];

    [...]
    > strcat(last, result);

    [...]
    > for(j=0;j<2560;j++) printw("%c", result[j]);


    Besides the trouble with the zero-length last[] array, you have also used the
    contents of the result[] array without ever putting anything in it. Did you
    get the order of arguments of strcat() backwards? It's hard to figure out
    what you thought you were doing, with so many mistakes everywhere.

    It'll be impossible to determine what's going on with your curses code until
    you clean up this wacky string-handling code.

    I seem to recall using endwin() and refresh() to leave and re-enter curses.
    def_prog_mode() and reset_prog_mode() are probably not necessary.

    --
    Alan Curry
     
    Alan Curry, Nov 11, 2010
    #4
  5. Ian Collins <> writes:
    > On 11/12/10 08:23 AM, Keith Thompson wrote:
    >> dachteruit<> writes:
    >>> First of all I'm a newbie in C programming and I was playing around
    >>> with ncurses.
    >>> I've followed the instructions as described in
    >>> http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/misc.html#TEMPLEAVE to
    >>> leave curses mode temporarily. So I wrote the following:
    >>> #include<stdio.h>
    >>> #include<ncurses.h>
    >>> #include<string.h>
    >>>
    >>> int main(){

    >>
    >> "int main(void)" is better. ("int main()" would be preferred in C++.)
    >>
    >>> int i=0, j=2560;
    >>> char last, result[j];

    >>
    >> This declares last and results as variable-length arrays (VLAs), which
    >> means that the length of the array can be determined at run time. Since
    >> the values you're using are always going to be the same, there's not
    >> much point in using VLAs rather than just plain fixed-length arrays.

    >
    > Also note that a VLA with a length of zero isn't going to do you much
    > good and is probably the cause of many of your problems!


    Yes, I pointed that out in the part you snipped.

    The behavior is undefined; see C99 6.7.5.2p5.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 11, 2010
    #5
  6. dachteruit

    Ian Collins Guest

    On 11/12/10 11:09 AM, Keith Thompson wrote:
    > Ian Collins<> writes:
    >> On 11/12/10 08:23 AM, Keith Thompson wrote:
    >>> dachteruit<> writes:
    >>>> First of all I'm a newbie in C programming and I was playing around
    >>>> with ncurses.
    >>>> I've followed the instructions as described in
    >>>> http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/misc.html#TEMPLEAVE to
    >>>> leave curses mode temporarily. So I wrote the following:
    >>>> #include<stdio.h>
    >>>> #include<ncurses.h>
    >>>> #include<string.h>
    >>>>
    >>>> int main(){
    >>>
    >>> "int main(void)" is better. ("int main()" would be preferred in C++.)
    >>>
    >>>> int i=0, j=2560;
    >>>> char last, result[j];
    >>>
    >>> This declares last and results as variable-length arrays (VLAs), which
    >>> means that the length of the array can be determined at run time. Since
    >>> the values you're using are always going to be the same, there's not
    >>> much point in using VLAs rather than just plain fixed-length arrays.

    >>
    >> Also note that a VLA with a length of zero isn't going to do you much
    >> good and is probably the cause of many of your problems!

    >
    > Yes, I pointed that out in the part you snipped.
    >
    > The behavior is undefined; see C99 6.7.5.2p5.


    Ah sorry, I didn't spot that bit.

    --
    Ian Collins
     
    Ian Collins, Nov 11, 2010
    #6
  7. dachteruit

    dachteruit Guest

    On Nov 11, 10:33 pm, (Alan Curry) wrote:
    > In article <..com>,
    >
    > dachteruit  <> wrote:
    > >int main(){
    > >    int i=0, j=2560;
    > >    char last, result[j];

    > [...]
    > >            strcat(last, result);

    > [...]
    > >    for(j=0;j<2560;j++) printw("%c", result[j]);

    >
    > Besides the trouble with the zero-length last[] array, you have also used the
    > contents of the result[] array without ever putting anything in it. Did you
    > get the order of arguments of strcat() backwards? It's hard to figure out
    > what you thought you were doing, with so many mistakes everywhere.
    >
    > It'll be impossible to determine what's going on with your curses code until
    > you clean up this wacky string-handling code.
    >
    > I seem to recall using endwin() and refresh() to leave and re-enter curses.
    > def_prog_mode() and reset_prog_mode() are probably not necessary.


    Ok, now I know what I can do tomorrow.
    It seems like I still have a lot to learn.

    Nico
     
    dachteruit, Nov 12, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Burkert
    Replies:
    1
    Views:
    449
    Jarek Zgoda
    Jan 18, 2004
  2. William Morgan
    Replies:
    0
    Views:
    551
    William Morgan
    Feb 12, 2005
  3. Kenneth McDonald

    Question on curses and ncurses

    Kenneth McDonald, Aug 22, 2008, in forum: Ruby
    Replies:
    1
    Views:
    118
    Harish Chandra
    Sep 12, 2008
  4. Kenneth McDonald
    Replies:
    8
    Views:
    286
    James Gray
    Sep 26, 2008
  5. Kenneth McDonald
    Replies:
    1
    Views:
    166
    Nit Khair
    Sep 27, 2008
Loading...

Share This Page