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

M

mark.e.nelson

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
 
A

A. Sinan Unur

(e-mail address removed) wrote in @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
 
F

Flash Gordon

(e-mail address removed) wrote:

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

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top