英文:
problem with understanding a especial Structure in C
问题
我在理解以下C语言结构方面遇到问题!我理解结构体以及它们的用途,但是这个似乎对我来说有点奇怪,我无法看到标准的结构体形式,就像在C学习网站(比如www.geeksforgeeks.org)中描述的那样!
关于这个结构体的代码没有更多的信息!
typedef struct sIMasterConnection* IMasterConnection;
struct sIMasterConnection {
bool (*isReady) (IMasterConnection self);
bool (*sendASDU) (IMasterConnection self, CS101_ASDU asdu);
bool (*sendACT_CON) (IMasterConnection self, CS101_ASDU asdu, bool negative);
bool (*sendACT_TERM) (IMasterConnection self, CS101_ASDU asdu);
void (*close) (IMasterConnection self);
int (*getPeerAddress) (IMasterConnection self, char* addrBuf, int addrBufSize);
CS101_AppLayerParameters (*getApplicationLayerParameters) (IMasterConnection self);
void* object;
};
英文:
i have problem with understanding below struct in c language !
i understand what is a struct and what it use for but this one seems weird for me and i can't see the standard form of struct like it's described in c learning websites like www.geeksforgeeks.org !
It's nothing more in code about this struct !
typedef struct sIMasterConnection* IMasterConnection;
struct sIMasterConnection {
bool (*isReady) (IMasterConnection self);
bool (*sendASDU) (IMasterConnection self, CS101_ASDU asdu);
bool (*sendACT_CON) (IMasterConnection self, CS101_ASDU asdu, bool negative);
bool (*sendACT_TERM) (IMasterConnection self, CS101_ASDU asdu);
void (*close) (IMasterConnection self);
int (*getPeerAddress) (IMasterConnection self, char* addrBuf, int addrBufSize);
CS101_AppLayerParameters (*getApplicationLayerParameters) (IMasterConnection self);
void* object;
};
答案1
得分: 0
除了这个数据成员之外,结构的所有数据成员都是函数指针。
例如,这个数据成员
bool (*isReady) (IMasterConnection self);
声明了一个指向返回类型为 bool
,参数类型为 IMasterConnection
(这是一个指向 struct sIMasterConnection
的别名类型)的函数指针。这里的参数 self
的类型是指向 struct sIMasterConnection
类型对象的指针。
为了更清楚地理解,考虑一个简单的演示程序,其中声明了一个函数指针。
#include <stdio.h>
void f( void )
{
puts( "Hello, World!" );
}
int main( void )
{
void ( *pf )( void ) = f;
pf();
}
程序输出结果是
Hello, World!
类似于声明标量变量 pf
的方式,你可以声明一个结构的数据成员。
#include <stdio.h>
void f( void )
{
puts( "Hello, World!" );
}
int main( void )
{
struct A
{
void ( *pf )( void );
};
struct A a = { .pf = f };
a.pf();
}
英文:
All data members of the structure except this one
void* object;
are pointers to functions.
For example this data member
bool (*isReady) (IMasterConnection self);
declares a pointer to function that has return type bool
and one parameter of the type IMasterConnection
that is an alias for the pointer type declared like
typedef struct sIMasterConnection* IMasterConnection;
That is the parameter self
has the type of a pointer to an object of the type struct sIMasterConnection
.
To make it clear consider a simple demonstration program where there is declared a pointer to a function.
#include <stdio.h>
void f( void )
{
puts( "Hello, World!" );
}
int main( void )
{
void ( *pf )( void ) = f;
pf();
}
The program output is
Hello, World!
A similar way as there is declared the saclar variable pf
you may declare a data member of a struture.
#include <stdio.h>
void f( void )
{
puts( "Hello, World!" );
}
int main( void )
{
struct A
{
void ( *pf )( void );
};
struct A a = { .pf = f };
a.pf();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论