473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Mar 31 '06 #1
2 2206
ma***********@gmail.com 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 <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
Mar 31 '06 #2
ma***********@gmail.com 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
Mar 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
7
by: James Vanns | last post by:
Sounds nasty doesn't it!! Well it's kinda what I need to do! I have an external C struct (external to the C++ project/classes etc.) which is wants a function ptr assigned to one of it's members: ...
4
by: Pushkar Pradhan | last post by:
I need to pass a struct by reference to a function: The struct: typedef struct tifftags { uint32 imageWidth, ...; ... } TIFFTAGS; typedef TIFFTAGS * TIFFTAGS_PTR; main() { .....
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.