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

Pointers to functions in structures trubble!

Hi guys and gals,

Haven't been in here for a good decade or so!
I'm having a braid-dead moment that is lasting for
a couple of days. Declaring pointers to functions
witrhin structures and accessing same....

This doesnt work. I thought it should. The compiler disagrees with me!
gcc32

-- lots of stuff like this

sparqbeat.c: In function `__life':
sparqbeat.c:1188: request for member `primary' in something not a
structure or union
sparqbeat.c:1190: request for member `open_server_socket' in something
not a structure or union
sparqbeat.c:1192: request for member `bind_server_socket' in something
not a structure or union
sparqbeat.c:1194: request for member `server' in something not a
structure or union
sparqbeat.c:1198: request for member `verbose' in something not a
structure or union
sparqbeat.c:1200: request for member `display' in something not a
structure or union
sparqbeat.c:1202: request for member `log_it' in something not a
structure or union
sparqbeat.c:1204: request for member `itoa' in something not a
structure or union
sparqbeat.c:1204: request for member `identity' in something not a
structure or union
sparqbeat.c:1204: request for member `scratch' in something not a
structure or union
sparqbeat.c:1205: request for member `me' in something not a structure
or union
sparqbeat.c:1210: request for member `verbose' in something not a
structure or union
sparqbeat.c:1212: request for member `display' in something not a
structure or union
sparqbeat.c:1214: request for member `log_it' in something not a
structure or union

---------

Any hints appreciated. I read the "variable arguments" thread,
and went back a few months looking for something similar.
Google returned 15,200,000 hits for
pointers to functions within structures
typedef struct __sparqbeat
{
int identity ; // my pid i
int child_identity ; // forked child pid
int error_number ; // current error state
int normal_pulse ; // pulse at rest, operation normal
int tachycardic ; // fast pulse, trying to recover
int port ; // what port to connect?
int time_of_death ; // how long 'till pronounced dead?
int beats ; // number of heartbeats since birth
int failures ; // number of failures since birth
int socket_fd ; // socket descriptor
int child_socket_fd ; // socket descriptor after fork
struct sockaddr_ini client_socket ; // client socket
structure
struct sockaddr_in server_socket ; // server socket structure

// 'encapsulated' functions

boolean ( *logging ) ( char * log_name ) = &__logging
;
void ( *log_it ) ( const char * err1, const char * err2,
const char * err3, const char * err4 ) = &__log_it ;
boolean ( *security_check) ( void ) = &__security_check ;
boolean ( *inherit ) ( void ) = &__inherit ;
boolean ( *birth ) ( int number_arguments, char ** arguments ) =
&__birth ;
boolean ( *triage ) ( void ) = &__triage ;
boolean ( *pulse ) ( void ) = &__pulse ;
boolean ( *life ) ( void ) = &__life ;
void ( *death ) ( char * eulogy ) = &__death ;
void ( *display ) ( char * message ) = &__display ;
void ( *usage ) ( void ) = &__usage ;
boolean ( *open_server_socket) ( void ) = __open_server_socket ;
boolean ( *bind_server_socket) ( void ) = &__bind_server_socket ;
boolean ( *open_client_socket) ( void ) = &__open_client_socket ;
boolean ( *connect_server) ( void ) = &__connect_server ;
void ( *shutdown_server) ( void ) = &__shutdown_server ;
void ( *shutdown_client) ( void ) = &__shutdown_client ;
void ( *server) ( void ) = &__server ;
void ( *client) ( void ) = &__client ;
void ( *send) ( int the_socket, char * ack ) = &__send ;
int ( *receive) ( int the_socket, char * peer_message ) = &__receive ;
char * ( *itoa) ( int value, char *string, int radix ) = &__itoa ;
boolean ( *ping_network) ( void ) = &__ping_network ;
boolean ( *ping_twin) ( void ) = &__ping_twin ;
boolean ( *read_config_file) ( void ) = &__read_config_file ;
} sparqbeat ;

sparqbeat * heartbeat ;
[....]

//-----------------------------
int main(int argc, char **argv)
{
if ( i_am_god() ) // need to be root for the
{ // tricks in this bad boy...
if ( new( heartbeat ) ) // allocate a new instance of the
{ // application object. Use heap
if ( heartbeat->birth( argc, argv ) ) // can the object birth
itself?
{ // if so, a long and happy life
heartbeat->life() ; // life handles the normal (should
be)
} // non-stop pulsing
else // but then again,
{ // birth failed, probably configuration
heartbeat->death( "Failed Birth" ) ; // other routines
will have written to
} // the .log files with detailed
} // information (depending on DEBUG level)
else // -----------------------------
{ // memory allocation failed, use the
printf( "Initial malloc FAIL" ) ; // routine printf. The
object
} // (and functions) are dead.(obviously)
} // -----------------------------
else // not root
{ // tell them
printf( "You must be root to run this utility" ) ;
} // and quit
// -----------------------------
clean_up() ; // look after memory, sockets and signals
// -----------------------------
return TRUE ; // ABEND is always abnormal, seek yea
} // the log files........bye
Ta.

Mark.

Jul 13 '06 #1
12 1981
ad******@addinall.org wrote:
Hi guys and gals,

Haven't been in here for a good decade or so!
I'm having a braid-dead moment that is lasting for
a couple of days. Declaring pointers to functions
witrhin structures and accessing same....
<snip>
boolean ( *logging ) ( char * log_name ) = &__logging;
Boing! You can't do an assignment in a structure declaration.

--
Ian Collins.
Jul 13 '06 #2

Ian Collins wrote:
ad******@addinall.org wrote:
Hi guys and gals,

Haven't been in here for a good decade or so!
I'm having a braid-dead moment that is lasting for
a couple of days. Declaring pointers to functions
witrhin structures and accessing same....
<snip>
boolean ( *logging ) ( char * log_name ) = &__logging;

Boing! You can't do an assignment in a structure declaration.
Very true! Stupid me.

I'm still having trubbles 'though after fixing that stuff-up.

The function.

//---------------------------------------
boolean new( sparqbeat * new_heartbeat )

// Allocate heap space for the major application object
// Return pass or fail condition

{
if ( new_heartbeat = malloc( sizeof( sparqbeat ) ) )
{
bzero( new_heartbeat, sizeof( sparqbeat ) ) ;
new_heartbeat->life = __life ;
// new_heartbeat->life = &__life ;
// new_heartbeat->life = &(__life);
return TRUE ;
}
else
{
return FALSE ;
}
}

t.c: In function `new':
t.c:177: warning: assignment from incompatible pointer type

In all instances.

Running the program just produces a segmentation fault.
I have done this before on a SCO system (good SCO in the old
days, not new EVIL SCO ;-), but I don't remember it being
this difficult.

Any futher pointers appreciated.
Cheese,
Mark.
>
--
Ian Collins.
Jul 13 '06 #3
ad******@addinall.org wrote:
Ian Collins wrote:
>>
>>>boolean ( *logging ) ( char * log_name ) = &__logging;

Boing! You can't do an assignment in a structure declaration.


Very true! Stupid me.

I'm still having trubbles 'though after fixing that stuff-up.

The function.

//---------------------------------------
boolean new( sparqbeat * new_heartbeat )

// Allocate heap space for the major application object
// Return pass or fail condition

{
if ( new_heartbeat = malloc( sizeof( sparqbeat ) ) )
{
bzero( new_heartbeat, sizeof( sparqbeat ) ) ;
new_heartbeat->life = __life ;
// new_heartbeat->life = &__life ;
// new_heartbeat->life = &(__life);
return TRUE ;
}
else
{
return FALSE ;
}
}

t.c: In function `new':
t.c:177: warning: assignment from incompatible pointer type
What's the prototype for __life? Generally not a good idea to use '__'
at the beginning of a function name.

--
Ian Collins.
Jul 13 '06 #4

Ian Collins wrote:
ad******@addinall.org wrote:
Ian Collins wrote:
What's the prototype for __life? Generally not a good idea to use '__'
at the beginning of a function name.
Yeah. I thought an earlier prblem may have been name duplication
so.....

I got it to compile by...
typedef struct __sparqbeat
{
int identity ; // my pid i
int child_identity ; // forked child pid
int error_number ; // current error state
int normal_pulse ; // pulse at rest,
operation normal
int tachycardic ; // fast pulse, trying to
recover
int port ; // what port to connect?
int time_of_death ; // how long 'till
pronounced dead?
int beats ; // number of heartbeats
since birth
int failures ; // number of failures sice
birth
int verbose ; // noisy reporting
int do_as_i_say ; // force operation
regardless
int auto_flag ; // failover automatically
int primary ; // server or client?

void (*life ) ( void ) ;
void (*death ) ( char * eulogy ) ;

} sparqbeat ;

sparqbeat * heartbeat ; // major application object

//------------------------------
void __death( char * eulogy )

{
}
//--------------
void __life( void )

{
//if ( THIS->primary ) // if this is a server
{ // then
}
}

//---------------------------------------
boolean new( sparqbeat * new_heartbeat )

// Allocate heap space for the major application object
// Return pass or fail condition

{
printf(" in new" ) ;
if ( new_heartbeat = malloc( sizeof( sparqbeat ) ) )
{
printf(" after malloc " ) ;
bzero( new_heartbeat, sizeof( sparqbeat ) ) ;
printf(" after bzero " ) ;
new_heartbeat->life = &__life ;
new_heartbeat->death = &__death ;
printf(" after pointer allocationi " ) ;

return TRUE ;
}
else
{
return FALSE ;
}
}

//-----------------------------
int main(int argc, char **argv)
{
printf("Made it in " ) ;
if ( new( heartbeat ) ) // allocate a new
instance of the
{ // application object.
Use heap
heartbeat->life() ; // life handles the normal
(should be)
} // non-stop pulsing
else // but then again,
{ // birth failed, probably
configuration
printf( "Initial malloc FAIL" ) ; // routine printf. The
object
} // (and functions) are
dead.(obviously)
//
-----------------------------
return TRUE ; // ABEND is always
abnormal, seek yea

} // the log
files........bye
//--------------- EOF
--------------------------------------------------

So... it compiles and links without errors just on a simple

cc t.c -o t

Try to run it

../t
Segmentation fault.

Something is stuffed and I think it may be me! ;-)

Cheese,
Mark.
>
--
Ian Collins.
Jul 13 '06 #5
ad******@addinall.org wrote:
Ian Collins wrote:
>>ad******@addinall.org wrote:
>>>Ian Collins wrote:

What's the prototype for __life? Generally not a good idea to use '__'
at the beginning of a function name.


Yeah. I thought an earlier prblem may have been name duplication
so.....

I got it to compile by...

<snipped code>
>
new_heartbeat->life = &__life ;
The & isn't requited and you realy should remove the double underscore
from your names. All identifiers starting with double underscore are
reserved.
So... it compiles and links without errors just on a simple

cc t.c -o t

Try to run it

../t
Segmentation fault.

Something is stuffed and I think it may be me! ;-)
Star with a simple test case, like

#include <stdio.h>

void f() { puts("hello\n"); }

struct X
{
void (*fn)();
};

int
main()
{
struct X x;

x.fn = f;

x.fn();
}

--
Ian Collins.
Jul 13 '06 #6

Ian Collins wrote:
ad******@addinall.org wrote:
Ian Collins wrote:
>ad******@addinall.org wrote:

Ian Collins wrote:
What's the prototype for __life? Generally not a good idea to use '__'
at the beginning of a function name.

Yeah. I thought an earlier prblem may have been name duplication
so.....

I got it to compile by...
<snipped code>

new_heartbeat->life = &__life ;

The & isn't requited and you realy should remove the double underscore
from your names. All identifiers starting with double underscore are
reserved.
So... it compiles and links without errors just on a simple

cc t.c -o t

Try to run it

../t
Segmentation fault.

Something is stuffed and I think it may be me! ;-)
Star with a simple test case, like

#include <stdio.h>

void f() { puts("hello\n"); }

struct X
{
void (*fn)();
};

int
main()
{
struct X x;

x.fn = f;

x.fn();
}
Yeah. I did that, well I stripped out MOST of the functionality
of my application and tinkered with a shell.

I'm back playing with the big code now (it's a peer to peer
data synchronisation and high-availability failover utility).

For the life of me I dunno what is wrong/

All this works

typedef struct __sparqbeat
{
// ints and stuff

boolean ( * birth ) ( int number_arguments, char **
arguments );
/// etc
} sparqbeat ;

main()
{
sparqbeat.life = __life ;
sparqbeat.life() ;
// yada yada yada
}

no problems.

I wanted to have my object on the heap, hence

sparqbeat *heartbeat ;

and

sparqbeat->life = __life ;
sparqbeat->life() ;

# Segmentation error

?????

Dunno. I'm using the dot notation (running out of time
to fiddle ;-) but I'd like to know what is going on....

Cheers mate,
owe ya a beer! :-)


--
Ian Collins.
Jul 13 '06 #7
ad******@addinall.org wrote:
boolean new( sparqbeat * new_heartbeat )

// Allocate heap space for the major application object
// Return pass or fail condition
How will the calling function be able to find the allocated object if
you don't return its address?

Jul 13 '06 #8
addinall wrote:
Ian Collins wrote:
>ad******@addinall.org wrote:
>>Ian Collins wrote:

ad******@addinall.org wrote:

Ian Collins wrote:
>

What's the prototype for __life? Generally not a good idea to use
'__' at the beginning of a function name.
Yeah. I thought an earlier prblem may have been name duplication
so.....

I got it to compile by...

<snipped code>
>>>
new_heartbeat->life = &__life ;

The & isn't requited and you realy should remove the double
underscore from your names. All identifiers starting with double
underscore are reserved.
>>So... it compiles and links without errors just on a simple

cc t.c -o t

Try to run it

../t
Segmentation fault.

Something is stuffed and I think it may be me! ;-)
Star with a simple test case, like

#include <stdio.h>

void f() { puts("hello\n"); }

struct X
{
void (*fn)();
};

int
main()
{
struct X x;

x.fn = f;

x.fn();
}

Yeah. I did that, well I stripped out MOST of the functionality
of my application and tinkered with a shell.

I'm back playing with the big code now (it's a peer to peer
data synchronisation and high-availability failover utility).

For the life of me I dunno what is wrong/

All this works

typedef struct __sparqbeat
{
// ints and stuff

boolean ( * birth ) ( int number_arguments, char **
arguments );
/// etc
} sparqbeat ;

main()
{
sparqbeat.life = __life ;
sparqbeat.life() ;
// yada yada yada
}

no problems.

I wanted to have my object on the heap, hence

sparqbeat *heartbeat ;

I'm guessing you *do* heartbeat = malloc(sizeof(*heartbeat)) somewhere
then - right?

and

sparqbeat->life = __life ;
sparqbeat->life() ;

# Segmentation error

?????

Dunno. I'm using the dot notation (running out of time
to fiddle ;-) but I'd like to know what is going on....


--
==============
Not a pedant
==============
Jul 13 '06 #9
ad******@addinall.org wrote:
boolean new( sparqbeat * new_heartbeat )

// Allocate heap space for the major application object
// Return pass or fail condition

{
if ( new_heartbeat = malloc( sizeof( sparqbeat ) ) )
{
bzero( new_heartbeat, sizeof( sparqbeat ) ) ;
new_heartbeat->life = __life ;
// new_heartbeat->life = &__life ;
// new_heartbeat->life = &(__life);
return TRUE ;
}
else
{
return FALSE ;
}
}
When the compiler errors get fixed:

You allocate store and then throw away the pointer - when the function
exits the value of `new_heartbeat` becomes inaccessible.

--
Chris "seeker" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

Jul 13 '06 #10
pemo wrote:
addinall wrote:
>Ian Collins wrote:
>>ad******@addinall.org wrote:
Ian Collins wrote:

ad******@addinall.org wrote:
>
>Ian Collins wrote:
>>
>
What's the prototype for __life? Generally not a good idea to use
'__' at the beginning of a function name.
Yeah. I thought an earlier prblem may have been name duplication
so.....

I got it to compile by...
<snipped code>

new_heartbeat->life = &__life ;

The & isn't requited and you realy should remove the double
underscore from your names. All identifiers starting with double
underscore are reserved.

So... it compiles and links without errors just on a simple

cc t.c -o t

Try to run it

../t
Segmentation fault.

Something is stuffed and I think it may be me! ;-)

Star with a simple test case, like

#include <stdio.h>

void f() { puts("hello\n"); }

struct X
{
void (*fn)();
};

int
main()
{
struct X x;

x.fn = f;

x.fn();
}

Yeah. I did that, well I stripped out MOST of the functionality
of my application and tinkered with a shell.

I'm back playing with the big code now (it's a peer to peer
data synchronisation and high-availability failover utility).

For the life of me I dunno what is wrong/

All this works

typedef struct __sparqbeat
{
// ints and stuff

boolean ( * birth ) ( int number_arguments, char **
arguments );
/// etc
} sparqbeat ;

main()
{
sparqbeat.life = __life ;
sparqbeat.life() ;
// yada yada yada
}

no problems.

I wanted to have my object on the heap, hence

sparqbeat *heartbeat ;


I'm guessing you *do* heartbeat = malloc(sizeof(*heartbeat)) somewhere
then - right?

Oh - I see new() does this - so shouldn't you be passing &heatbeat to new(),
i.e., a pointer to a pointer?

>
>and

sparqbeat->life = __life ;
sparqbeat->life() ;

# Segmentation error

?????

Dunno. I'm using the dot notation (running out of time
to fiddle ;-) but I'd like to know what is going on....
--
==============
Not a pedant
==============
Jul 13 '06 #11

pemo wrote:
pemo wrote:
addinall wrote:
Ian Collins wrote:

ad******@addinall.org wrote:
Ian Collins wrote:

ad******@addinall.org wrote:

Ian Collins wrote:
>

What's the prototype for __life? Generally not a good idea to use
'__' at the beginning of a function name.
Yeah. I thought an earlier prblem may have been name duplication
so.....

I got it to compile by...
<snipped code>

new_heartbeat->life = &__life ;

The & isn't requited and you realy should remove the double
underscore from your names. All identifiers starting with double
underscore are reserved.

So... it compiles and links without errors just on a simple

cc t.c -o t

Try to run it

../t
Segmentation fault.

Something is stuffed and I think it may be me! ;-)

Star with a simple test case, like

#include <stdio.h>

void f() { puts("hello\n"); }

struct X
{
void (*fn)();
};

int
main()
{
struct X x;

x.fn = f;

x.fn();
}
Yeah. I did that, well I stripped out MOST of the functionality
of my application and tinkered with a shell.

I'm back playing with the big code now (it's a peer to peer
data synchronisation and high-availability failover utility).

For the life of me I dunno what is wrong/

All this works

typedef struct __sparqbeat
{
// ints and stuff

boolean ( * birth ) ( int number_arguments, char **
arguments );
/// etc
} sparqbeat ;

main()
{
sparqbeat.life = __life ;
sparqbeat.life() ;
// yada yada yada
}

no problems.

I wanted to have my object on the heap, hence

sparqbeat *heartbeat ;

I'm guessing you *do* heartbeat = malloc(sizeof(*heartbeat)) somewhere
then - right?


Oh - I see new() does this - so shouldn't you be passing &heatbeat to new(),
i.e., a pointer to a pointer?
Arrrrggggghhhhhhh!

Excuse me whilst I throw myself from the window ;-)))))))

Mark.
>
and

sparqbeat->life = __life ;
sparqbeat->life() ;

# Segmentation error

?????

Dunno. I'm using the dot notation (running out of time
to fiddle ;-) but I'd like to know what is going on....

--
==============
Not a pedant
==============
Jul 14 '06 #12

Chris Dollin wrote:
ad******@addinall.org wrote:
boolean new( sparqbeat * new_heartbeat )

// Allocate heap space for the major application object
// Return pass or fail condition

{
if ( new_heartbeat = malloc( sizeof( sparqbeat ) ) )
{
bzero( new_heartbeat, sizeof( sparqbeat ) ) ;
new_heartbeat->life = __life ;
// new_heartbeat->life = &__life ;
// new_heartbeat->life = &(__life);
return TRUE ;
}
else
{
return FALSE ;
}
}

When the compiler errors get fixed:

You allocate store and then throw away the pointer - when the function
exits the value of `new_heartbeat` becomes inaccessible.
Yes. Thanks all for spotting my stupidity (senility ;-)

See ya in another 10 years!

Regards,
Mark.
>
--
Chris "seeker" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/
Jul 14 '06 #13

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

Similar topics

94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
4
by: Konstantin Shemyak | last post by:
I have a big structure tree. All leaves are scalar values (no pointers). Present are arrays, structures and unions. I want to be able to store/read the content of the structure in/from a file, and...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
6
by: Sam | last post by:
Hi everyone I'm confused by these declarations: struct hostent *he; server *s = (server*)malloc(sizeof(server)); /* in the code * he = gethostbyname(name);
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
6
by: Computer Wizard | last post by:
Hello, I am really scrwed up with the following stuff: struct employee { char* employee_name; }; struct department {
25
by: hifrnds007 | last post by:
how the function poiners avoids the usage of switch cases?
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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:
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.