首页 > 编程知识 正文

函数名是地址吗,c语言函数的传值和传地址

时间:2023-05-06 15:41:05 阅读:149327 作者:80

可能会显示以下代码:

/* * * * * * * * * * * * * * * * * * *

#包含

#包含

#包含

void test () )

{

打印(123456 (n ) );

}

intmain(intargc,char *argv[] ) ) ) ) ) ) ) ) ) int main ) ) ) int main ) ) ) int argc,char *argv[] ) ) ) ) ) ) ) int

{

printf(0x%x(n ),test );

printf(0x%x(n ),test );

}

[[emailprotected] pht]# ./a.out

0x8048328

0x8048328

在运算符的原意中,要求操作数是对象,但函数名称不是对象。 (函数是对象。 )。 本来,test是非法的,但很久以前一些编译器就允许这样做了。

由于c/c标准的制定者已经发展了对象的概念,所以也承认了test的合法性。

因此,对于test和test,应该理解为test是函数的起始地址,其类型表示void (),test表示函数的test对象的地址。

类型为void(* ) ),因此test和test表示的地址值相同,但类型不同。 test是函数,test表达式的值是指针!

与这个问题相似的是在数组名称中取地址。

int a[100];

printf(%p(n ),a );

printf(%p(n ),a[0];

打印值相同。

但是,数组名a是指具有100个int型的组数;

a[0]是指元素a[0]。

也就是说,他们的值相同,但所指类型不同。

标准用那个rationale说明了这个问题。 摘录如下。

6.5.3.2地址sandindirectionoperators

someimplementationshavenotallowedtheoperatobeappliedtoanarrayorafunction。

(theconstructwaspermittedinearlyversionsofc,then later made optional.) the c89语言

committeendorsedtheconstructsinceitisunambiguous,and since data abstraction is

增强byallowingtheimportantoperatoapplyuniformlytoanyaddressableentity。

原文: 3359 www.cn blogs.com/still-smile/p/11592292.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。