指向结构中的功能指针! [英] Pointers to functions in structures trubble!

查看:58
本文介绍了指向结构中的功能指针!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计和女孩们,


Haven在这里待了好十年左右!

我有一条辫子 - 死亡的那一刻持续了几天。声明指向函数

结构并访问它们....


这不起作用。我认为应该。编译器不同意我的看法!

gcc32


- 很多这样的东西


sparqbeat.c:在函数`__life'':

sparqbeat.c:1188:请求成员`primary''的东西不是

结构或联盟

sparqbeat.c:1190:请求成员`open_server_socket''

不是结构或联合

sparqbeat.c:1192:请求成员`bind_server_socket''在某事物中

不是结构或联盟

sparqbeat.c:1194:请求成员`server''的东西不是

结构或工会

sparqbeat.c:1198:请求成员`verbose''的东西不是

结构或联盟

sparqbeat.c:1200 :请求成员'显示''的东西不是

结构或联盟

sparqbeat.c:1202:请求成员`log_it''的东西不是

结构或联合

sparqbeat.c:1204:请求mem ber` itoa''在某些东西不是

结构或联盟

sparqbeat.c:1204:请求成员'身份''在某些东西不是

结构或联盟

sparqbeat.c:1204:请求成员`scratch''的东西不是

结构或联盟

sparqbeat.c:1205:请求成员'我'在非结构的东西

或者工会

sparqbeat.c:1210:请求成员`verbose''in某些东西不是

结构或联盟

sparqbeat.c:1212:请求成员`display''在某些东西不是

结构或联合

sparqbeat.c:1214:请求成员`log_it''的东西不是

结构或联盟


- -------


任何提示都表示赞赏。我读了变量参数。线程,

并且回过头几个月寻找类似的东西。

Google返回了15,200,000次点击指示

指向结构内功能的指针
typedef struct __sparqbeat

{

int identity; //我的pid我

int child_identity; //分叉子pid

int error_number; //当前错误状态

int normal_pulse; //静止脉搏,手术正常

int tachycardic; //快速脉冲,试图恢复

int port; //连接的端口是什么?

int time_of_death; //发送死了多长时间?

int beats; //自出生以来的心跳次数

int failure; //自出生以来的失败次数

int socket_fd; //套接字描述符

int child_socket_fd; // fork之后的套接字描述符

struct sockaddr_ini client_socket; //客户端套接字

结构

struct sockaddr_in server_socket; //服务器套接字结构


//''封装''函数


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_ser ver_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;

} s parqbeat;


sparqbeat * heartbeat;

[....]


// ---- -------------------------

int main(int argc,char ** argv)

{

如果(i_am_god())//需要是root的

{//欺骗这个坏男孩......

if(new(heartbeat))//分配

{//应用程序对象的新实例。使用堆

if(heartbeat-> birth(argc,argv))//对象可以生成

本身吗?

{//如果是这样,一个漫长而幸福的生活

heartbeat-> life(); //生活处理正常(应该是



} //不间断脉冲

其他//但是又一次,

{//出生失败,可能是配置

心跳 - >死亡(失败的出生); //其他例程

将写入

} // .log文件,其中包含详细的

} //信息(取决于DEBUG级别) )

else // -----------------------------

{ //内存分配失败,使用

printf(初始malloc FAIL); //例程printf。

对象

} //(和函数)已经死了。(显然)

} // -------- ---------------------

else // not root

{//告诉他们

printf(你必须是root才能运行这个实用程序);

} //并退出

// ------- ----------------------

clean_up(); //照顾记忆,插座和信号

// -----------------------------

返回TRUE; // ABEND总是不正常,请求

} //日志文件........再见

Ta。


马克。

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.

推荐答案

ad ****** @ addinall.org 写道:

大家好朋友们,


Haven在这里待了好十年左右!

我有一个辫子死亡的时刻,持续时间为每天几美元。声明指向函数

结构和访问它们....
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>

<snip>


boolean(* logging)(char * log_name)=& __ logging;
boolean ( *logging ) ( char * log_name ) = &__logging;



Boing!你不能在结构声明中做作业。


-

Ian Collins。

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

--
Ian Collins.




Ian Collins写道:

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

嗨伙计和女孩,


避风港''在这里待了十年左右!

我有一个辫子死的时刻,持续时间为每天几美元。声明指向函数

结构并访问它们....
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>

<snip>


boolean(* logging)(char * log_name)=& __ logging;
boolean ( *logging ) ( char * log_name ) = &__logging;



Boing!你不能在结构声明中做任务。


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



非常真实!愚蠢的我。


虽然在修好了这个东西之后我仍然有'trubbles'。


这个功能。


// ---------------------------------------

boolean new(sparqbeat * new_heartbeat)


//为主要应用程序对象分配堆空间

//返回传递或失败的条件


{

if(new_heartbeat = malloc(sizeof(sparqbeat)))

{

bzero(new_heartbeat,sizeof(sparqbeat));

new_heartbeat-> life = __life;

// new_heartbeat-> life =& __ life;

// new_heartbeat-> life =&(__ life);

返回TRUE;

}

else

{

返回FALSE;

}

}


tc :在函数new中:

tc:177:警告:从不兼容的指针类型分配


在所有情况下。

运行程序只会生成一个seg心理错误。

我之前在SCO系统上做过这个(旧的SCO在旧的
天,而不是新的EVIL SCO ;-),但我不是记住这很难

这个很难。


任何进一步的指示赞赏。

奶酪,

马克。

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。
>
--
Ian Collins.


ad******@addinall.org 写道:

Ian Collins写道:
Ian Collins wrote:

>>
>>

>>> boolean(* logging)(char * log_name)=& __ logging;
>>>boolean ( *logging ) ( char * log_name ) = &__logging;


Boing!你不能在结构声明中做任务。


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




非常真实!愚蠢的我。


虽然在修好了这个东西之后我仍然有'trubbles'。


这个功能。


// ---------------------------------------

boolean new(sparqbeat * new_heartbeat)


//为主要应用程序对象分配堆空间

//返回传递或失败的条件


{

if(new_heartbeat = malloc(sizeof(sparqbeat)))

{

bzero(new_heartbeat,sizeof(sparqbeat));

new_heartbeat-> life = __life;

// new_heartbeat-> life =& __ life;

// new_heartbeat-> life =&(__ life);

返回TRUE;

}

else

{

返回FALSE;

}

}


tc :在函数`new'':

tc:177:警告:从不兼容的指针类型分配



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



__life的原型是什么?在函数名称的开头使用''__''

通常不是一个好主意。


-

伊恩柯林斯。

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

--
Ian Collins.


这篇关于指向结构中的功能指针!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆