passing a pointer to a struct to a function that uses ncurses

Discussion in 'C Programming' started by mark.e.nelson@gmail.com, Mar 31, 2006.

  1. Guest

    Hi,

    I am trying to pass a pointer to a struct to a function that uses the
    data in the struct, and also happens to use ncurses. I always get a
    segmentation violation when the program exits. I have experimented
    with passing a pointer to a struct as an argument to a function that
    does not use ncurses and that seems to work fine.

    Can anyone provide any advice? I apologise if this is the wrong place
    to ask - I am just coming back to C after many years and I am not sure
    (can't remember) if my problem is a C problem or a ncurses problem.

    code:
    run.c
    #include "myform.h"
    #include <string.h>
    #include <malloc.h>

    int main()
    {
    form_data data;
    char *name = "Mark Nelson\0";
    char *address = "1 Main Road\0";

    // data = (form_data) calloc(1, sizeof(form_data));
    strcpy(data.name, name);
    strcpy(data.address, address);
    display_form(&data);
    printf("back in run");
    return (0);
    }

    myform.h
    #include <form.h>

    typedef struct {
    char name[40];
    char address[40];
    } form_data;

    int display_form(form_data *);

    myform.c
    #include "myform.h"

    int display_form(form_data *data)
    {
    FIELD *field[2];
    FORM *my_form;
    int ch;

    // initialise curses
    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);

    // initialise the fields
    field[0] = new_field(1, 40, 4, 18, 0, 0);
    field[1] = new_field(1, 40, 6, 18, 0, 0);
    field[2] = NULL;

    // set field options
    set_field_back(field[0], A_UNDERLINE);
    field_opts_off(field[0], O_AUTOSKIP);
    field_opts_off(field[0], O_BLANK);

    set_field_back(field[1], A_UNDERLINE);
    field_opts_off(field[1], O_AUTOSKIP);
    field_opts_off(field[1], O_BLANK);

    // put data in fields
    set_field_buffer(field[0], 0, data->name);
    set_field_buffer(field[1], 0, data->address);

    // create and post the form
    my_form = new_form(field);
    post_form(my_form);
    refresh();

    set_current_field(my_form, field[0]); // set focus
    form_driver(my_form, REQ_END_LINE);
    mvprintw(4, 10, "Name:");
    mvprintw(6, 10, "Address:");
    mvprintw(8, 10, "F1 to exit");
    refresh();

    // loop through user requests
    while ((ch = getch()) != KEY_F(1))
    {
    switch (ch)
    {
    case KEY_DOWN:
    // go to next field
    form_driver(my_form, REQ_NEXT_FIELD);
    // move to end of data
    form_driver(my_form, REQ_END_LINE);
    break;
    case KEY_UP:
    // go to previous field
    form_driver(my_form, REQ_PREV_FIELD);
    form_driver(my_form, REQ_END_LINE);
    break;
    default:
    form_driver(my_form, ch);
    break;
    }
    }

    // unpost form and free memory
    printf("x");
    unpost_form(my_form);
    printf("x");
    free_form(my_form);
    printf("x");
    free_field(field[0]);
    printf("x");
    free_field(field[1]);
    printf("x");

    endwin();
    return (0);
    }

    Makefile:
    COMPILER = gcc -Wall -g
    LIBS = -lncurses -lform
    EXECUTABLE = run
    OBJECT = run.o myform.o

    $(EXECUTABLE): $(OBJECT)
    $(COMPILER) -o $(EXECUTABLE) $(OBJECT) $(LIBS)

    %.o: %.c
    $(COMPILER) -o $*.o -c $*.c

    clean:
    rm *.o $(EXECUTABLE).exe

    all: $(OBJECT)
    $(COMPILER) -o $(EXECUTABLE) $(OBJECT) $(LIBS)

    I have compiled and tested this on cygwin/gcc on XP and on RHEL AS 3.0
    with the same results.

    Thanks for your help,
    Mark Nelson
    , Mar 31, 2006
    #1
    1. Advertising

  2. wrote in news:1143805978.415339.184090
    @j33g2000cwa.googlegroups.com:

    > run.c
    > #include "myform.h"
    > #include <string.h>
    > #include <malloc.h>


    malloc.h is non-standard, use stdlib.h.


    > int main()


    int main(void) is better.

    > {
    > form_data data;


    You have already declared data here to be of type form_data. This has
    already allocated space for data.

    > char *name = "Mark Nelson\0";
    > char *address = "1 Main Road\0";


    Why the \0's at the end?

    >
    > // data = (form_data) calloc(1, sizeof(form_data));


    Why is this line here? Is it commented out in the code that crashes?

    > strcpy(data.name, name);
    > strcpy(data.address, address);


    struct form_data data = { "Mark Nelson", "1 Main Road" };

    The rest is platform-dependent stuff which you might want to take a Unix
    related group maybe.

    Sinan
    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)
    A. Sinan Unur, Mar 31, 2006
    #2
    1. Advertising

  3. Flash Gordon Guest

    wrote:

    In addition to what A. Sinan Unur said...

    <snip>

    > int display_form(form_data *data)
    > {
    > FIELD *field[2];


    This defines an array of 210 pointers to FIELD...

    > FORM *my_form;
    > int ch;
    >
    > // initialise curses
    > initscr();
    > cbreak();
    > noecho();
    > keypad(stdscr, TRUE);
    >
    > // initialise the fields
    > field[0] = new_field(1, 40, 4, 18, 0, 0);


    This writes to the first element of field...

    > field[1] = new_field(1, 40, 6, 18, 0, 0);


    This writes to the second element of field...

    > field[2] = NULL;


    This write to the ... oops, there is no third element to write to!

    <snip>
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
    Flash Gordon, Mar 31, 2006
    #3
    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. Ole
    Replies:
    4
    Views:
    587
    Michael Wojcik
    Oct 26, 2004
  2. beetle
    Replies:
    2
    Views:
    891
    beetle
    Jan 25, 2005
  3. Zero
    Replies:
    16
    Views:
    642
    Barry Schwarz
    Nov 19, 2005
  4. Kenneth McDonald
    Replies:
    1
    Views:
    138
    Nit Khair
    Sep 27, 2008
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    452
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page