对,形参有地址,是一个变量
形参和局部变量唯一的区别就是,形参是调用函数的时候初始化,而局部变量需要你自己手动初始化
形参就是个局部变量,只是初始化的时间比局部变量早一些
sh-5.2$ cat main.c
#include <stdio.h>
void a(int *data) {
printf("a: &data = %p\n", &data);
printf("a: data = %p\n", data);
}
void b(int data[]) {
printf("b: &data = %p\n", &data);
printf("b: data = %p\n", data);
}
int main(void) {
int data[10];
printf("main: &data = %p\n", &data);
printf("main: data = %p\n", data);
a(data);
b(data);
return 0;
}
sh-5.2$ ./main
main: &data = 0x766981f09030
main: data = 0x766981f09030
a: &data = 0x766981e09020
a: data = 0x766981f09030
b: &data = 0x766981e09060
b: data = 0x766981f09030
sh-5.2$
学过汇编语言的话就很好理解了
int main(void) {
11d3: 55 pushq %rbp
11d4: 48 89 e5 movq %rsp,%rbp
11d7: 48 83 ec 30 subq $0x30,%rsp
11db: 64 48 8b 04 25 28 00 movq %fs:0x28,%rax
11e2: 00 00
11e4: 48 89 45 f8 movq %rax,-0x8(%rbp)
11e8: 31 c0 xorl %eax,%eax
int data[10];
printf("main: &data = %p\n", &data);
11ea: 48 8d 45 d0 leaq -0x30(%rbp),%rax
11ee: 48 89 c6 movq %rax,%rsi
11f1: 48 8d 05 46 0e 00 00 leaq 0xe46(%rip),%rax # 203e <_IO_stdin_used+0x3e>
11f8: 48 89 c7 movq %rax,%rdi
11fb: b8 00 00 00 00 movl $0x0,%eax
1200: e8 3b fe ff ff callq 1040 <printf@plt>
printf("main: data = %p\n", data);
1205: 48 8d 45 d0 leaq -0x30(%rbp),%rax
1209: 48 89 c6 movq %rax,%rsi
120c: 48 8d 05 3d 0e 00 00 leaq 0xe3d(%rip),%rax # 2050 <_IO_stdin_used+0x50>
1213: 48 89 c7 movq %rax,%rdi
1216: b8 00 00 00 00 movl $0x0,%eax
121b: e8 20 fe ff ff callq 1040 <printf@plt>
a(data);
1220: 48 8d 45 d0 leaq -0x30(%rbp),%rax
1224: 48 89 c7 movq %rax,%rdi
1227: e8 1d ff ff ff callq 1149 <a>
b(data);
122c: 48 8d 45 d0 leaq -0x30(%rbp),%rax
1230: 48 89 c7 movq %rax,%rdi
1233: e8 56 ff ff ff callq 118e <b>
return 0;
1238: b8 00 00 00 00 movl $0x0,%eax
}
123d: 48 8b 55 f8 movq -0x8(%rbp),%rdx
1241: 64 48 2b 14 25 28 00 subq %fs:0x28,%rdx
1248: 00 00
124a: 74 05 je 1251 <main+0x7e>
124c: e8 df fd ff ff callq 1030 <__stack_chk_fail@plt>
1251: c9 leaveq
1252: c3 retq
void a(int *data) {
1149: 55 pushq %rbp
114a: 48 89 e5 movq %rsp,%rbp
114d: 48 83 ec 10 subq $0x10,%rsp
1151: 48 89 7d f8 movq %rdi,-0x8(%rbp)
printf("a: &data = %p\n", &data);
1155: 48 8d 45 f8 leaq -0x8(%rbp),%rax
1159: 48 89 c6 movq %rax,%rsi
115c: 48 8d 05 a1 0e 00 00 leaq 0xea1(%rip),%rax # 2004 <_IO_stdin_used+0x4>
1163: 48 89 c7 movq %rax,%rdi
1166: b8 00 00 00 00 movl $0x0,%eax
116b: e8 d0 fe ff ff callq 1040 <printf@plt>
printf("a: data = %p\n", data);
1170: 48 8b 45 f8 movq -0x8(%rbp),%rax
1174: 48 89 c6 movq %rax,%rsi
1177: 48 8d 05 95 0e 00 00 leaq 0xe95(%rip),%rax # 2013 <_IO_stdin_used+0x13>
117e: 48 89 c7 movq %rax,%rdi
1181: b8 00 00 00 00 movl $0x0,%eax
1186: e8 b5 fe ff ff callq 1040 <printf@plt>
}
118b: 90 nop
118c: c9 leaveq
118d: c3 retq
|