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



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

.



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... int main{ ... using struct and typedef. ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: [RFC][PATCH 1/6] memcg: fix pre_destory handler
    ... returns struct cgroup of id. ... SwapCgroup uses array of "pointer" to record the owner of swaps. ... struct cgroup_id is freed by RCU. ... changed interface from pointer to "int" ...
    (Linux-Kernel)
  • gcc, aliasing rules and unions
    ... struct B {int x, y;}; ... A struct pointer can be converted to another ... Also I don't know what the "effective type" of a union member is. ...
    (comp.lang.c)