第120页K& R. [英] page 120 K&R
问题描述
大家好,
这个小胡说八道的功能是尝试理解功能
指针。
/ *** ****** /
#include< stdio.h>
void p(int);
int main(int argc,const char * argv []){
void(* q)(int)= p;
q(50 ); / *关于这个的问题* /
返回0;
}
void p(int i){
int j = i,k = 1;
while(i-- 0)
printf("打印我%d次(迭代%2d)\ n",j,k ++);
}
/ ***** /
从我在p 120上的阅读中我预计对p的调用是* q(50)而不是
q(50)但* q给出错误。释义K& R,q是指向
函数的指针,* q是函数,(* q)(参数)是对它的调用。
可能有人解释我错过了什么?
谢谢。
Hi All,
this little nonsense function is an attempt to understand function
pointers.
/*********/
#include <stdio.h>
void p(int);
int main (int argc, const char * argv[]) {
void (*q)(int) = p;
q(50); /* Question about this */
return 0;
}
void p(int i){
int j = i, k=1;
while ( i-- 0)
printf("Print me %d times (iteration %2d)\n", j, k++);
}
/*****/
From my reading on p 120 I expected the call to p to be *q(50) and not
q(50) but *q gives an error. Paraphrasing K&R, q is a pointer to a
function, *q is the function and (*q)( arguments) is the call to it.
Could someone explain what I am missing?
Thank you.
推荐答案
11月26日晚上10点02分,mdh< m ... @ comcast.netwrote:
On Nov 26, 10:02 pm, mdh <m...@comcast.netwrote:
大家好,
这个小小的废话函数试图理解函数
指针。
/ ********* /
#include< stdio .h>
void p(int);
int main(int argc,const char * argv []){
void(* q)(int)= p;
q(50); / *关于这个的问题* /
返回0;
}
void p(int i){
int j = i,k = 1;
while(i-- 0)
printf( 打印我%d次(迭代%2d)\ n,j,k ++);
}
/ ***** /
从我在120页的阅读中我预计p的调用为* q(50)而不是
q(50)但是* q给出错误。释义K& R,q是指向
函数的指针,* q是函数,(* q)(参数)是对它的调用。
可能有人解释我错过了什么?
Hi All,
this little nonsense function is an attempt to understand function
pointers.
/*********/
#include <stdio.h>
void p(int);
int main (int argc, const char * argv[]) {
void (*q)(int) = p;
q(50); /* Question about this */
return 0;
}
void p(int i){
int j = i, k=1;
while ( i-- 0)
printf("Print me %d times (iteration %2d)\n", j, k++);
}
/*****/
From my reading on p 120 I expected the call to p to be *q(50) and not
q(50) but *q gives an error. Paraphrasing K&R, q is a pointer to a
function, *q is the function and (*q)( arguments) is the call to it.
Could someone explain what I am missing?
C:\ tmp> oops
输入p with 7
打印7次(迭代1次) )
打印7次(迭代2)
打印7次(迭代3)
打印7次(迭代4)< br $>
打印7次(迭代5次)
打印7次(迭代6次)
打印7次(迭代7次)
用3输入p
打印我3次(迭代1)
打印我3次(迭代2)
打印我3次(迭代3)
输入p与4
打印我4次(迭代1)
打印我4次(迭代2 )
打印我4次(迭代3)
打印我4次(迭代4)
C:\ tmp> ;输入oops.c
#include< stdio.h>
void p(int);
int main(int argc,const char * argv [])
{
void(* q)(int)= p;
q (7);
(* q)(3);
p(4);
返回0;
}
void p(int i)
{
int j = i,
k = 1;
printf(输入%s与%d \ n,__ FUNCTION __,i);
while(i-- 0)
printf(打印我%d次(迭代%2d)\ n,j,k ++);
}
/ **** * /
C:\tmp>oops
entered p with 7
Print me 7 times (iteration 1)
Print me 7 times (iteration 2)
Print me 7 times (iteration 3)
Print me 7 times (iteration 4)
Print me 7 times (iteration 5)
Print me 7 times (iteration 6)
Print me 7 times (iteration 7)
entered p with 3
Print me 3 times (iteration 1)
Print me 3 times (iteration 2)
Print me 3 times (iteration 3)
entered p with 4
Print me 4 times (iteration 1)
Print me 4 times (iteration 2)
Print me 4 times (iteration 3)
Print me 4 times (iteration 4)
C:\tmp>type oops.c
#include <stdio.h>
void p(int);
int main(int argc, const char *argv[])
{
void (*q) (int) = p;
q(7);
(*q) (3);
p(4);
return 0;
}
void p(int i)
{
int j = i,
k = 1;
printf("entered %s with %d\n", __FUNCTION__, i);
while (i-- 0)
printf("Print me %d times (iteration %2d)\n", j, k++);
}
/*****/
mdh写道,On 27/11/07 06:02:
mdh wrote, On 27/11/07 06:02:
大家好,
这个小胡说八道的功能是尝试理解功能
指针。
/ ****** *** /
#include< stdio.h>
void p(int);
int main(int argc,const char * argv []){
void(* q)(int)= p;
q(50); / *关于这个的问题* /
Hi All,
this little nonsense function is an attempt to understand function
pointers.
/*********/
#include <stdio.h>
void p(int);
int main (int argc, const char * argv[]) {
void (*q)(int) = p;
q(50); /* Question about this */
< snip>
<snip>
从我在第120页的阅读中我期望对p的调用为* q(50)而不是
q(50)但* q给出错误。释义K& R,q是指向
函数的指针,* q是函数,(* q)(参数)是对它的调用。
可能有人解释我错过了什么?
From my reading on p 120 I expected the call to p to be *q(50) and not
q(50) but *q gives an error. Paraphrasing K&R, q is a pointer to a
function, *q is the function and (*q)( arguments) is the call to it.
Could someone explain what I am missing?
你遗漏了一些东西。首先,在大多数情况下,函数的名称是衰变。到一个函数指针(这就是为什么你没有使用&取得p的地址),当你调用
函数时,你实际上是在调用它它通过函数指针。显然
q是一个函数指针,所以通过函数指针调用你可以通过它调用
。
接下来就是那个( )比q更紧密地绑定到*,到* q(50)意味着调用q
将50传递给它并取消引用q返回的(指针)值,
显然是一个错误,因为q没有返回任何值,更不用说
指针了。所以你需要(* q)(50)取消引用q然后调用它指向的
函数。即便如此,这只能起作用,因为C的奇怪的b / b $ b规则意味着取消引用q然后得到
自动转换回函数指针。作为一个副作用,你可以使用($ q)b(50)。它也使得应用和到一个
函数名称毫无意义。
-
Flash Gordon
You are missing a few things. The first is that in most situations the
name of a function "decays" to a function pointer (which is why you did
not need to use & to take the address of p) and when you are calling a
function you are actually calling it via the function pointer. Obviously
q is a function pointer so as calls are via function pointers you can
call through it.
Next is that () binds more tightly to q than *, to *q(50) means call q
passing 50 to it and dereference the (pointer) value returned by q,
which is obviously an error as q does not return any value, let alone a
pointer. So you need (*q)(50) to dereference q and then call the
function it points to. Even so this only works because of the strange
rules of C which mean that having dereferenced q it then gets
automatically converted back to a function pointer. As a side effect you
could just as well use (*******q)(50). It also makes applying & to a
function name pointless.
--
Flash Gordon
On 11月27日上午12:51,Flash Gordon< s ... @ flash-gordon.me.ukwrote:
On Nov 27, 12:51 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
mdh写道,2007年11月27日06:02:
mdh wrote, On 27/11/07 06:02:
int main(int argc,const char * argv []){
void(* q )(int)= p;
q(50); / *关于这个的问题* /
int main (int argc, const char * argv[]) {
void (*q)(int) = p;
q(50); /* Question about this */
< snip>
<snip>
>
你遗漏了一些东西。首先,在大多数情况下,函数的名称是衰变。到一个函数指针(这就是为什么你没有使用&取得p的地址),当你调用
函数时,你实际上是在调用它它通过函数指针。
>
You are missing a few things. The first is that in most situations the
name of a function "decays" to a function pointer (which is why you did
not need to use & to take the address of p) and when you are calling a
function you are actually calling it via the function pointer.
让我确保我理解你。一旦定义了函数,该函数的
地址就等同于它的名字。所以对该函数的任何
引用只是对它的地址的引用。
(类似于引用数组的方式?)
Let me make sure I understand you. Once a function is defined, the
address of that function becomes equivalent to it''s name. So any
reference to that function is simply a reference to it''s address.
(Kind of similar to the way an array is referenced?)
接下来是()绑定到q比*更紧密,到* q(50)表示调用q
将50传递给它并取消引用q返回的(指针)值,
这显然是一个错误
Next is that () binds more tightly to q than *, to *q(50) means call q
passing 50 to it and dereference the (pointer) value returned by q,
which is obviously an error
我一直被关联性绊倒...谢谢你。
因为q不会返回任何值,更不用说
I keep getting tripped up by associativity....thank you.
as q does not return any value, let alone a
指针。所以你需要(* q)(50)取消引用q然后调用它指向的
函数。即便如此,这只能起作用,因为C的奇怪的b / b $ b规则意味着取消引用q然后得到
自动转换回函数指针。
pointer. So you need (*q)(50) to dereference q and then call the
function it points to. Even so this only works because of the strange
rules of C which mean that having dereferenced q it then gets
automatically converted back to a function pointer.
好的......谢谢。离涅ana更近了一步!!!
Ok...thank you. One step closer to nirvana!!!
这篇关于第120页K& R.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!