结构和变量函数 [英] Structures and Variable Functions

查看:80
本文介绍了结构和变量函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

变量函数可以在C中实现,如果是,如何?


另外,你可以将函数或函数原型作为一个成员用于

结构?


-

我只是海市蜃楼。

-

comp.lang.c.moderated - 审核地址: cl**@plethora.net

解决方案

kelvSYC< ke ***** @ no.email.shaw.ca>写道:

可以在C中实现变量函数,如果是,如何实现?


请定义变量函数。

此外,您可以将函数或函数原型作为
结构中的成员吗?




不,但你可能有一个指向函数的指针。

-

int main(void ){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr( ); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);}返回0;}


kelvSYC写道:
< blockquote class =post_quotes>可以在C中实现变量函数,如果是,如何实现?


我查看了Maclennan的索引,以及龙书中的索引,

变量函数。没有骰子。你是什​​么意思?

另外,你有一个函数或函数原型作为
结构中的成员吗?




你可以将一个函数指针作为结构成员。


-

Richard Heathfield: bi **** @ eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton

-

comp.lang.c.moderated - 审核地址: cl ** @ plethora.net


kelvSYC< ke ***** @ no.email.shaw.ca>写道:

#可以在C中实现变量函数,如果是,如何实现?


是的。


#另外,你有一个函数或函数原型作为

#结构的成员吗?


是的。

(cd / tmp

cat<<'':eof''> tc

#include< stdio.h>

#include< stdlib.h>

typedef int(* func)(char * s);

typedef struct {func f; char * s ;}对象;


int X(char * s){printf(" X-%sX \ n",s); return 1;}

int Y(char * s){printf(" Y-%sY \ n",s); return 2;}


object choose(char * s){

object o; os = s;

switch(* s){

case''x'':of = X; break;

case''y'':of = Y; break;

}

return o;

}

int apply(object o){return of(os);}


int main(int N,char ** P){

int r = apply(c hoose(P [1]));

printf(" return%d \ n");

返回0;

}

:eof

cc tc

a.out xyzzy)


X-xyzzy-X

返回0


-

Derk Gwen http://derkgwen.250free.com/html/index.html

但我相信这一点。

-

comp.lang.c.moderated - 审核地址: cl ** @ plethora .net


Can variable functions be implemented in C, and if so, how?

Also, can you have a function or a function prototype as a member in a
structure?

--
I am only a mirage.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net

解决方案

kelvSYC <ke*****@no.email.shaw.ca> writes:

Can variable functions be implemented in C, and if so, how?
Please define "variable function".
Also, can you have a function or a function prototype as a member in a
structure?



No, but you may have a pointer to a function.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


kelvSYC wrote:

Can variable functions be implemented in C, and if so, how?
I checked the index in Maclennan, and the index in the Dragon book, for
"variable function". No dice. What do you mean by it?
Also, can you have a function or a function prototype as a member in a
structure?



You can have a function pointer as a structure member.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
--
comp.lang.c.moderated - moderation address: cl**@plethora.net


kelvSYC <ke*****@no.email.shaw.ca> wrote:
# Can variable functions be implemented in C, and if so, how?

Yes.

# Also, can you have a function or a function prototype as a member in a
# structure?

Yes.

(cd /tmp
cat <<'':eof'' >t.c
#include <stdio.h>
#include <stdlib.h>
typedef int (*func)(char *s);
typedef struct {func f; char *s;} object;

int X(char *s) {printf("X-%s-X\n",s); return 1;}
int Y(char *s) {printf("Y-%s-Y\n",s); return 2;}

object choose(char *s) {
object o; o.s = s;
switch (*s) {
case ''x'': o.f = X; break;
case ''y'': o.f = Y; break;
}
return o;
}
int apply(object o) {return o.f(o.s);}

int main(int N,char **P) {
int r = apply(choose(P[1]));
printf("return %d\n");
return 0;
}
:eof
cc t.c
a.out xyzzy)

X-xyzzy-X
return 0

--
Derk Gwen http://derkgwen.250free.com/html/index.html
But I do believe in this.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net


这篇关于结构和变量函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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