473,378 Members | 1,679 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,378 software developers and data experts.

passing a structure and casting it void -- help please

I am trying to call a function that is defined as .. function x (char
*item , void *key)

I have a struct :

struct guide_options {
mvp_widget_t *mywidget;
char *myitem;
};

main () {

struct guide_options guideptr;

// then fill the struct
guideptr.mywidget = widget2;
guideptr.myitem = buf;

x ( (char*)i , (void*)&guideptr);

But it seg faults. Can someone point me in a direction.

Thank you, Mike

Aug 9 '06 #1
10 1770
Mike wrote:
I am trying to call a function that is defined as .. function x (char
*item , void *key)

I have a struct :

struct guide_options {
mvp_widget_t *mywidget;
char *myitem;
};

main () {

struct guide_options guideptr;

// then fill the struct
guideptr.mywidget = widget2;
guideptr.myitem = buf;

x ( (char*)i , (void*)&guideptr);
What is i and why are you casting it? Why cast &guideptr?
But it seg faults. Can someone point me in a direction.
Not without the guts of x().

--
Ian Collins.
Aug 9 '06 #2
>
But it seg faults. Can someone point me in a direction.
The best way to debug this issue would be to use a debugging tool (GDB)
to analyse your code with the core dump. This should tell you the line
where the fault is and will make it easier to nail down the exact
issue.
Make sure you compile your program with the -g option before running
the binary.

/A

Aug 9 '06 #3
Mike wrote:
But it seg faults. Can someone point me in a direction.
You might find the error very quickly by running your program under
strace or truss.

Plenty of times, segv errors suppress the output or can mislead you in
other ways, and I often find truss shows me more.

At the source level, you might use a static checking utility like lint,
and some of us in the 21st century are using splint (www.splint.org).

If you are trying to debug a problem involving your use of malloc/free,
on a unix-ish environment, you might find electric fence useful. You
link to it, and it basically overrides malloc() and free() and lets you
watch your program die by its own hand in the debugger.

It's not always obvious where a segfault is really happening. You may
think you know where the problem is, and beat your head flat trying to
fix it, when the real bug is somewhere else entirely.
Aug 9 '06 #4
On Wed, 9 Aug 2006 01:02:45 UTC, "Mike" <md****@yahoo.comwrote:
I am trying to call a function that is defined as .. function x (char
*item , void *key)

I have a struct :

struct guide_options {
mvp_widget_t *mywidget;
char *myitem;
};

main () {

struct guide_options guideptr;

// then fill the struct
guideptr.mywidget = widget2;
guideptr.myitem = buf;

x ( (char*)i , (void*)&guideptr);

But it seg faults. Can someone point me in a direction.
You are lying to the compiler and the compiler gets its revange.

Never ever use a cast when you are not exactly knows what you are
doing. And you shows that you are not about ab knows what you are
doing.

Learn the differencer between pointer and pointer to pointer.

Set your compiler options to the hardes possible warning level and try
to run your program only if the compier will not output a single error
or wearning.

And again: do not cast until you understunds what you are doing.
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 9 '06 #5
akina wrote:

But it seg faults. Can someone point me in a direction.

The best way to debug this issue would be to use a debugging tool
(GDB) to analyse your code with the core dump. This should tell you
the line where the fault is and will make it easier to nail down the
exact issue.
Make sure you compile your program with the -g option before running
the binary.
What did the OP say that made you assume he was using gcc?


Brian
Aug 9 '06 #6
Default User wrote:
akina wrote:
>
But it seg faults. Can someone point me in a direction.

The best way to debug this issue would be to use a debugging tool
(GDB) to analyse your code with the core dump. This should tell you
the line where the fault is and will make it easier to nail down the
exact issue.
Make sure you compile your program with the -g option before running
the binary.

What did the OP say that made you assume he was using gcc?
What did akina say that made you assume he assumed the OP was using gcc?
(It's possible that gcc was indeed assumed, but I only see an assumption of
a Unix-like system, which is a fairly reasonable one, considering the "seg
faults".)
Aug 9 '06 #7
Harald van Dijk wrote:
Default User wrote:
What did the OP say that made you assume he was using gcc?

What did akina say that made you assume he assumed the OP was using
gcc? (It's possible that gcc was indeed assumed, but I only see an
assumption of a Unix-like system, which is a fairly reasonable one,
considering the "seg faults".)

He mentioned gdb, with instructions on how build the executable to make
it compatible with that.


Brian
Aug 9 '06 #8
Default User wrote:
Harald van Dijk wrote:
>Default User wrote:
What did the OP say that made you assume he was using gcc?

What did akina say that made you assume he assumed the OP was using
gcc? (It's possible that gcc was indeed assumed, but I only see an
assumption of a Unix-like system, which is a fairly reasonable one,
considering the "seg faults".)


He mentioned gdb, with instructions on how build the executable to make
it compatible with that.
-g is one of the few compiler options specified by POSIX and is implemented
by icc, tendra, tinycc, gcc, sun's compiler and more, gdb was only
mentioned as an example of a debugger, and gdb also works with code
generated by other compilers. (gcc's debugging formats are not unique to
gcc.)
Aug 9 '06 #9
>
What did the OP say that made you assume he was using gcc?

Brian
"it seg faults" suggests that he is using a unix based system. And GCC
is a commonly used compiler in these systems. I guess there is no harm
in assuming this.
Probably I should have mentioned this in my post.

Anyways, it is "OP" who should say if my suggestion was useful or not.

Aug 9 '06 #10
Harald van Dijk wrote:
Default User wrote:
Harald van Dijk wrote:
Default User wrote:
What did the OP say that made you assume he was using gcc?

What did akina say that made you assume he assumed the OP was using
gcc? (It's possible that gcc was indeed assumed, but I only see an
assumption of a Unix-like system, which is a fairly reasonable one,
considering the "seg faults".)

He mentioned gdb, with instructions on how build the executable to
make it compatible with that.

-g is one of the few compiler options specified by POSIX and is
implemented by icc, tendra, tinycc, gcc, sun's compiler and more, gdb
was only mentioned as an example of a debugger, and gdb also works
with code generated by other compilers. (gcc's debugging formats are
not unique to gcc.)

Ok, granted. Let's amend to, "What did the OP say that made you assume
he was using UNIX?"


Brian
Aug 9 '06 #11

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...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
2
by: Malkocoglu | last post by:
I have a legacy DLL that i may have to use from C# The problem with this DLL is that some functions take void * as arguments Let me illustrate it in C/C++ first , let's say i have a database table...
3
by: iskeletor | last post by:
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #define STUDENT_NUMBER 68 #define ARRAY_LENGTH 10 struct node{ char Name,Surname; int data,no;
16
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
22
by: friend.05 | last post by:
typedef struct { int a; int b; int c; } ABC; typedef struct { int d;
9
by: nolonger | last post by:
Sample code:- typedef struct __Y { int a; char b; } Y; Y my_struct; Y *my_ptr_struct;
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.