473,224 Members | 1,602 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,224 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 1973
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.