struggling with void pointers/functions/structures

C

cirederf

Hello,

I am trying to run a code which compiles fine, but when I try to run it
I am facing a series of problems which are quite difficult to
understand. I am kind of new with C, the task is then a little
challenging.

First, the routine where it crashes starts like this

void read_instructions(E,argc,argv)
struct All_variables *E;

........E is then used in a series of functions and points to different
structures without causing any trouble

(E->problem_initial_fields)(E);
........the code seems to crash here

problem_initial_fields is defined in a header file that is included in
the routine

struct All_variables {
#include "convection_variables.h"
.....
void (* problem_initial_fields)();
.....
},

Can anybody explain to me what it actually means and have some
suggestions on the reasons for crashing. Is there a way I could print
out some of the values it seems to point to?

Thank you,
F.
 
K

Keith Thompson

cirederf said:
I am trying to run a code which compiles fine, but when I try to run it
I am facing a series of problems which are quite difficult to
understand. I am kind of new with C, the task is then a little
challenging.

First, the routine where it crashes starts like this

void read_instructions(E,argc,argv)
struct All_variables *E;

.......E is then used in a series of functions and points to different
structures without causing any trouble

(E->problem_initial_fields)(E);
.......the code seems to crash here

problem_initial_fields is defined in a header file that is included in
the routine

struct All_variables {
#include "convection_variables.h"
....
void (* problem_initial_fields)();
....
},

Can anybody explain to me what it actually means and have some
suggestions on the reasons for crashing. Is there a way I could print
out some of the values it seems to point to?

You have an old-style (non-prototype) declaration for your
read_instructions routine. This isn't wrong, but there's no reason to
do this. Rather than

void read_instructions(E, argc, argv)
struct All_variables *E;
int argc;
char **argv
{
...
}

you should use the more modern (i.e., since 1989):

void read_instructions(struct All_variables *E, int argc, char **argv)
{
...
}

This allows the compiler to do more checking.

Based on what little code you've shown us, the problem_initial_fields
member is a function pointer. You don't show us any code that assigns
a value to that pointer (nor do you show us the call to
read_instructions()). If it doesn't have a proper value, any attempt
to call through it will invoke undefined behavior, most likely a
crash.

There's no standard way to print the value of a function pointer, but
something like this:

printf("E->problem_initial_fields = %p\n",
(void*)E->problem_initial_fields);

may tell you something. The output will most likely look like a
hexadecimal number; if nothing else a null pointer should be
recognizable.

If you're still having trouble, see if you can create a small,
self-contained, compilable program that exhibits the problem.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top