function name address vs array name address

简介:
array 变量在编译时会替换成存储ARRAY数据的内存首地址。
所以 array:%p == &array:%p . 
例如 : 
[root@db-172-16-3-150 zzz]# cat f.c
#include <stdio.h>

char a[10] = "abcde";

int main() {
  fprintf(stdout, "a:%p, &a:%p\n", a, &a);
  return 0;
}
结果
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./f.c -o f && ./f
a:0x6008bc, &a:0x6008bc


function 名则更猛.
function:%p == &function:%p == *function:%p
说明函数名是个指针, 指针的内容就是这个指针的地址本身.
因此使用函数名, 或者*function 或者&function都可以调用这个函数.
例如 : 
[root@db-172-16-3-150 zzz]# cat f.c
#include <stdio.h>

int test() {
  fprintf(stdout, "test output\n");
  return 1;
}

int main() {
  int test1() {
    return 2;
  }

  int a = test1();
  int b = (&test)();  // 使用&test调用这个函数
  int c = (*test)();  // 使用*test调用这个函数

  fprintf(stdout, "a:%i, b:%i, c:%i\n", a, b, c);
  fprintf(stdout, "test:%p, &test:%p, *test:%p\n", test, &test, *test);
  fprintf(stdout, "test1:%p, &test1:%p, *test1:%p\n", test1, &test1, *test1);
  fprintf(stdout, "test1+1:%p, (&test1)+1:%p, (*test1)+1:%p\n", test1+1, (&test1)+1, (*test1)+1);
  return a;
}
结果
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./f.c -o f && ./f
test output
test output
a:2, b:1, c:1
test:0x400540, &test:0x400540, *test:0x400540
test1:0x400530, &test1:0x400530, *test1:0x400530
test1+1:0x400531, (&test1)+1:0x400531, (*test1)+1:0x400531  // 这里+1都加了一个字节, 而存储0x400531这个值需要2个字节, 所以看起来就不对劲了.
// 这些可能都是编译器使的障眼法. 函数指针可能另有所指, 那就是code区域. 

函数创建时, 这个函数指针将在内存的constants区域创建.  
从打印结构来看,  function:%p == &function:%p == *function:%p .但是这可能是编译器搞的鬼. 函数指针肯定是要执行函数体的code区域的, 
来看一幅图 : 
function name address vs array name address - 德哥@Digoal - The Heart,The World.
相关文章
SAP BRF+ function mode VS event mode
SAP BRF+ function mode VS event mode
118 0
SAP BRF+ function mode VS event mode
|
2月前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
2月前
|
存储 JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(上)
数组对象是使用单独的变量名来存储一系列的值。
|
3月前
|
存储 安全 Swift
在Swift中,数组(Array)
在Swift中,数组(Array)
29 3
|
4月前
|
Ruby
|
6月前
|
存储 Java 索引
【面试题精讲】ArrayList 和 Array(数组)的区别?
【面试题精讲】ArrayList 和 Array(数组)的区别?
|
5月前
|
算法 Python
数组倍增(Array Doubling
数组倍增(Array Doubling)是一种常见的算法技术,用于解决数组相关的查找、插入、删除等问题。该技术的核心思想是将数组的大小乘以 2,新数组的长度是原数组长度的两倍,然后将原数组中的元素复制到新数组中。在某些情况下,这种技术可以提高算法的效率,尤其是对于动态数据结构的问题。
124 1
|
2月前
Google Earth Engine(GEE)——reducer中进行array数组的获取和分析
Google Earth Engine(GEE)——reducer中进行array数组的获取和分析
33 0
|
3月前
|
Rust 索引 Windows
Rust 原始类型之数组array内置方法
Rust 原始类型之数组array内置方法
51 0
Rust 原始类型之数组array内置方法

热门文章

最新文章