有时看到以下的代码: c++
/*****************************/ #include<stdio.h> #include<string.h> #include <stdlib.h> void test(){printf("123456\n");} int main(intargc,char*argv[]) {printf("0x%x\n",test); printf("0x%x\n",&test);} [[emailprotected]pht]# ./a.out 0x80483280x8048328
按照&运算符原本的意义,它要求其操做数是一个对象,但函数名不是对象(函数是一个对象),原本&test是非法的,但好久之前有些编译器已经容许这样作, c/c++标准的制定者出于对象的概念已经有所发展的缘故,也认可了&test的合法性。程序员
若是你也想成为程序员,想要快速掌握编程,赶忙关注小编加入学习企鹅圈子吧!编程
里面有资深专业软件开发工程师,在线解答你的全部疑惑~编程语言入门“so easy”数组
资料包含:编程入门、游戏编程、课程设计等。app
免费学习书籍:编程语言
免费学习资料:函数
所以,对于test和&test你应该这样理解,test是函数的首地址,它的类型是void (),&test表示一个指向函数test这个对象的地址, 它的类型是void (*)(),所以test和&test所表明的地址值是同样的,但类型不同。学习
test是一个函数,&test表达式的值是一个指针! 跟此问题相似的还有对一个数组名取地址。 spa
int a[100]; printf("%p\n", a); printf("%p\n", &a[0]); 打印值同样。 可是数组名a,指向的是具备100个int类型的组数;设计
&a[0]指向的是元素a[0]。 即他们的值相同,但指向的类型不一样。 标准在其rationale中解释了这个问题,摘录以下:
6.5.3.2 Address and indirection operators Some implementations have not allowed the & operator to be applied to an array or a function. (The construct was permitted in early versions of C, then later made optional.) The C89 Language Committee endorsed the construct since it is unambiguous, and since data abstraction is enhanced by allowing the important & operator to apply uniformly to any addressable entity.