啊,他们有功能指针蓝调 [英] Ah've got them Function Pointer blues

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

问题描述

伙计,


我已经玩C程序25年了(不专业 -

自学),虽然我'之前我曾经使用过功能指针,我从来没有这么做过,因为我能够完全理解我现在想要做的事情。我不知道为什么 - 我对

语言的其他方面很好,但是当我读到函数指针时,我的大脑麻木了!


我想要一个结构数组,比如


struct FS

{< function pointer> ;;

<其他一些变量> ;;

};


我的问题是<未知类型的函数指针 - 在

function_structure [2]它可能是


int func_1(int,int){....}


在function_structure [5]中,它可能是


void func_2(char ** cpt){....}





我想直接初始化数组,比如

struct FS function_structure [] = {{....},{....}, ....,{....}};


....但是我对这些括号中的内容感到非常不稳定。我一直在玩它,但到目前为止还没有成功。


有可能吗?如何在结构中声明指针,以及如何
为其指定指针值?当我打电话给它时,如何从程序中传递参数

?有人可以给我一个非常简单的例子吗?


顺便说一下,这不是一个家庭作业的东西 - 我主要编写的小程序

同事们的工作。它提高了我的街头信誉

(geek-cred? - 好吧,不 - 我只是吹了这个想法!)。


顺便提一下,这个可能不是设计程序的最佳方式,所以我也会想问这个问题。

有人可以推荐一个新闻组吗? (程序架构不是真的C)


谢谢,


MikeC

-

竹子垃圾邮件机器人需要精神解密:


mijewen $ btconnect * com

$ = @

* = dot

Folks,

I''ve been playing with C programs for 25 years (not professionally -
self-taught), and although I''ve used function pointers before, I''ve never
got my head around them enough to be able to think my way through what I
want to do now. I don''t know why - I''m fine with most other aspects of the
language, but my brain goes numb when I''m reading about function pointers!

I would like to have an array of structures, something like

struct FS
{ <function pointer>;
<some other variables>;
};

My problem is that the <function pointeris of unknown type - in
function_structure[2] it may be

int func_1(int, int) {....}

in function_structure[5], it may be

void func_2(char **cpt) {....}

etc.

I would like to initialise the array directly, like
struct FS function_structure[] = {{ ....}, {....}, ...., {....}};

.... but I''m really shaky on what to put in those braces. I have been
playing with it, but so far unsuccessfully.

Is it possible? How do I declare the pointer in the structure, and how do I
assign a pointer value to it? And when I call it, how do I pass arguments
from the program? Could somebody give me a very simple example?

This isn''t a homework thing, by the way - I write mostly little programs
that get used at work by colleagues. It improves my street-cred
(geek-cred? - well, no - I''ve just blown that notion!).

Incidentally, this may not be the best way to design the program, so I''d
like to ask about that too.
Can anybody suggest a newsgroup? (program architecture isn''t really C)

Thanks,

MikeC
--
Mental decryption required to bamboozle spam robots:

mijewen$btconnect*com
$ = @
* = dot

推荐答案

btconnect * com
btconnect*com


= @

* = dot
= @
* = dot


" MikeC" < Mi ******* @ btconnect.comwrites:


< snip>
"MikeC" <Mi*******@btconnect.comwrites:

<snip>

我想拥有一系列结构,比如


struct FS

{<函数指针> ;;

<其他一些变量> ;;

};


我的问题是< ;函数指针未知类型 - 在

function_structure [2]它可能是


int func_1(int,int){....} ,它可能是


void func_2(char ** cpt){....}
< br $>

I would like to have an array of structures, something like

struct FS
{ <function pointer>;
<some other variables>;
};

My problem is that the <function pointeris of unknown type - in
function_structure[2] it may be

int func_1(int, int) {....}

in function_structure[5], it may be

void func_2(char **cpt) {....}

etc.



这不是问题,因为一种类型的函数指针可以将
转换为a另一种类型的函数指针。最简单的是

选择一个通用函数指针类型并使用它(类型

void(*)()通常用于此)或选择最多常见,因此

减少所需的转换次数。


请注意,您需要将指针转换为正确的类型

调用点。

That is not a problem since a function pointer of one type can be
converted to a function pointer of another type. The simplest is to
pick either a generic function pointer type and use that (the type
void (*)() is often used for this) or to pick the most common, thereby
cutting down the number of conversions needed.

Note that you need to convert the pointer to the right type at the
point of call.


我想直接初始化数组,比如

struct FS function_structure [] = {{。 ......},{....},....,{....}};
I would like to initialise the array directly, like
struct FS function_structure[] = {{ ....}, {....}, ...., {....}};



好​​吧,使用一个平淡的通用函数指针的概念并使用

typedef使语法简单:


typedef void function();


结构将如下所示:


struct FS {

功能* fp;

/ *其他数据* /

};


你设置了一个他们的数组是这样的:


struct FS function_structure [] = {

{(function *)func_1,/ *其他数据* /},

{(function *)func_2,/ *其他数据* /}

};


函数调用很糟糕。 function_structure [1] .fp是

类型''函数*''并且必须转换为''void(*)(char **)''所以你

需要写:


((void(*)(char **))function_structure [1] .fp)(argv);

Well, using the idea of a bland generic function pointer and using
typedefs to make the syntax simple:

typedef void function();

The struct will the look like this:

struct FS {
function *fp;
/* other data */
};

and you set up an array of them like this:

struct FS function_structure[] = {
{(function *)func_1, /* other data */},
{(function *)func_2, /* other data */}
};

The function call is horrible, though. function_structure[1].fp is of
type ''function *'' and must be converted to ''void (*)(char **)'' so you
need to write:

((void (*)(char **))function_structure[1].fp)(argv);


......但我对这些括号中的内容感到非常不稳定。我一直在玩它,但到目前为止还没有成功。
... but I''m really shaky on what to put in those braces. I have been
playing with it, but so far unsuccessfully.



您可以使用typedef为常用指针类型简化演员表

如果您愿意的话。

You can simplify the casts with typedefs for you common pointer types
if you like.


有可能吗?如何在结构中声明指针,以及如何
为其指定指针值?当我打电话给它时,如何从程序中传递参数

?有人可以给我一个非常简单的例子吗?
Is it possible? How do I declare the pointer in the structure, and how do I
assign a pointer value to it? And when I call it, how do I pass arguments
from the program? Could somebody give me a very simple example?



-

Ben。

--
Ben.


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

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