变量声明语法 [英] Variable declaration syntax
问题描述
大家好,
就在圣诞节前夕,Chris Torek给了我一些关于闭包的好建议:
< http://groups.google .co.nz / groups?selm = cqcl3k030vj%40news3.newsguy.com& output = gplain>
它包含一个名为fp的函数指针变量的声明:
void(* fp)(void *);
上面的语法是什么意思?我只能将它消化为void * fp,
即fp是指向void的指针。
谢谢,
Adam
Hi all,
Just before Christmas Chris Torek gave me some great advice about closures in C:
<http://groups.google.co.nz/groups?selm=cqcl3k030vj%40news3.newsguy.com&output =gplain>
It includes this declaration of a function pointer variable called fp:
void (*fp)(void *);
What does the syntax above mean? I can only digest it as void * fp,
i.e. fp is a pointer to void.
Thanks,
Adam
推荐答案
>就在圣诞节前夕,Chris Torek在C中给出了一些关于闭包的好建议:
> Just before Christmas Chris Torek gave me some great advice about closures in C:
< http://groups.google.co.nz/groups?selm = cqcl3k030vj%40news3.newsguy.com& output = gplain>
它包含了一个名为fp的函数指针变量的声明:
void(* fp)(void *);
上面的语法是什么意思?我只能将其消化为void * fp,
即fp是指向void的指针。
<http://groups.google.co.nz/groups?selm=cqcl3k030vj%40news3.newsguy.com&output =gplain>
It includes this declaration of a function pointer variable called fp:
void (*fp)(void *);
What does the syntax above mean? I can only digest it as void * fp,
i.e. fp is a pointer to void.
fp是一个指向函数的指针,该函数返回带参数的void清单
由空格*组成。
例如:
void do_naught(void *);
void do_aught(void *);
.....
if(bla)
fp = do_aught;
其他
fp = do_naught;
.....
/ *通过fp * /
(* fp)(地址)调用相应的函数;
.... 。
(* fp)(another_one);
.....
优点:您不必要求bla所有的时间,但设置
fp到你需要的功能。
如果你有C声明的问题,如
int (* p)[10];
vs
int * p [10];
你可以搜索程序cdecl,它什么都不做但
翻译乱码< - >英文,其中乱码是C声明。
干杯
Michael
-
电子邮件:我的是一个gmx点地址。
fp is a pointer to a function returning void with a parameter list
consisting of a void *.
example:
void do_naught (void *);
void do_aught (void *);
.....
if (bla)
fp = do_aught;
else
fp = do_naught;
.....
/* Call the appropriate function through fp */
(*fp)(address);
.....
(*fp)(another_one);
.....
Advantage: You have not to ask for "bla" all the time but set
fp to the function you need.
If you have problems with C declarations such as
int (*p)[10];
vs
int *p[10];
you could search for the program cdecl which does nothing but
translate gibberish<->english where gibberish are C declarations.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.
" Adam华纳" <我们**** @ consulting.net.nz>在留言中写道
news:pa ************************** @ consulting.net.n z ...
"Adam Warner" <us****@consulting.net.nz> wrote in message
news:pa**************************@consulting.net.n z...
大家好,
就在圣诞节前,Chris Torek给了我一些关于闭包的好建议
in C:
< http://groups.google.co.nz/groups? se ... wsguy.com& outp
ut = gplain>
它包含一个名为fp的函数指针变量的声明:
void(* fp) (void *);
上面的语法是什么意思?我只能将其消化为void * fp,
即fp是指向void的指针。
Hi all,
Just before Christmas Chris Torek gave me some great advice about closures in C: <http://groups.google.co.nz/groups?se...wsguy.com&outp
ut=gplain>
It includes this declaration of a function pointer variable called fp:
void (*fp)(void *);
What does the syntax above mean? I can only digest it as void * fp,
i.e. fp is a pointer to void.
''fp''是指向函数的指针采用类型为
''void *''的单个参数,并且不返回值。在网上搜索实用工具
称为''cdecl'',它可以接受声明并告诉你
的含义。
- Mike
''fp'' is a pointer to a function which takes a single argument of type
''void *'', and does not return a value. Search the web for a utility
called ''cdecl'', which can take a declaration and tell you what
it means.
-Mike
嗨Michael Mair,
On Sun,2005年1月2日13:10:56 +0100,Michael Mair写道:
Hi Michael Mair,
On Sun, 02 Jan 2005 13:10:56 +0100, Michael Mair wrote:
就在圣诞节前夕,Chris Torek给了我一些关于闭包的好建议:
< http://groups.google.co.nz/groups?selm = cqcl3k030vj %40news3.newsguy.com& output = gplain>
它包含一个名为fp的函数指针变量的声明:
void(* fp)(void *);
Just before Christmas Chris Torek gave me some great advice about closures in C:
<http://groups.google.co.nz/groups?selm=cqcl3k030vj%40news3.newsguy.com&output =gplain>
It includes this declaration of a function pointer variable called fp:
void (*fp)(void *);
fp是一个函数返回void的指针,参数列表由void *组成
。
fp is a pointer to a function returning void with a parameter list
consisting of a void *.
谢谢,我终于弄清楚了语法!如下所示:
#include< stdio.h>
#include< stdlib.h>
#include < time.h>
unsigned int uinc1(int x){return ++ x; }
unsigned int uinc2(int x){return x + = 2; }
int main(){
//种子随机数生成器,便携式
struct tm epoch;
epoch.tm_year = 27; epoch.tm_mon = 9-1; epoch.tm_mday = 4; epoch.tm_isdst = -1;
epoch.tm_hour = 0; epoch.tm_min = 0; epoch.tm_sec = 0;
clock_t current_clock = clock();
time_t current_time = time(& current_clock);
time_t time_since_epoch = mktime(& epoch);
double diff = difftime(current_time,time_since_epoch);
unsigned int seed =(unsigned int)diff;
printf(种子是%u \ n,种子);
srand(种子);
unsigned int(* fp)(int );
//注意:伪随机数生成器可能很差
if(rand()%2 == 0)fp = uinc1;否则fp = uinc2;
unsigned int sum = fp(1);
printf(" Sum is%u \ n",sum);
返回0;
}
BTW我发现cdecl是为Debian打包的。
问候,
Adam
Thanks, I''ve finally grokked the syntax! As demonstrated below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned int uinc1 (int x) { return ++x; }
unsigned int uinc2 (int x) { return x+=2; }
int main() {
//seed random number generator, portably
struct tm epoch;
epoch.tm_year=27; epoch.tm_mon=9-1; epoch.tm_mday=4; epoch.tm_isdst=-1;
epoch.tm_hour=0; epoch.tm_min=0; epoch.tm_sec=0;
clock_t current_clock=clock();
time_t current_time=time(¤t_clock);
time_t time_since_epoch=mktime(&epoch);
double diff=difftime(current_time, time_since_epoch);
unsigned int seed=(unsigned int) diff;
printf("Seed is %u\n", seed);
srand(seed);
unsigned int (* fp) (int);
//Note: The pseudo random number generator may be poor
if (rand()%2==0) fp=uinc1; else fp=uinc2;
unsigned int sum=fp(1);
printf("Sum is %u\n", sum);
return 0;
}
BTW I found cdecl was packaged for Debian.
Regards,
Adam
这篇关于变量声明语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!