$ gcc -g -o main main.c
$ ./main
a=0xbf9df198
a+1=0xbf9df19c
&a=0xbf9df198
&a+1=0xbf9df1c0   //注意 0xbf9df1c0 - 0xbf9df198 = 40D 正好是sizeof a的大小
$ cat main.c
#include <stdio.h>

int main()
{
    int a[10] = {
        1, 2, 3, 4, 5,
        6, 7, 8, 9, 10,
    };
    printf("a=%p\n", a);
    printf("a+1=%p\n", a + 1);
    printf("&a=%p\n", &a);
    printf("&a+1=%p\n", &a + 1);
    return 0;
}
$ gdb main
GNU gdb (GDB) 7.0-ubuntu
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/baozhifei/main...done.
(gdb) break main
Breakpoint 1 at 0x80483ed: file main.c, line 8.
(gdb) run
Starting program: /home/baozhifei/main 

Breakpoint 1, main () at main.c:8
8        };
(gdb) n
9        printf("a=%p\n", a);
(gdb) whatis a
type = int [10]
(gdb) whatis &a
type = int (*)[10]
(gdb) whatis &a + 1
type = int (*)[10]
(gdb) c
Continuing.
a=0xbffff3e8
a+1=0xbffff3ec
&a=0xbffff3e8
&a+1=0xbffff410

Program exited normally.
(gdb) quit

gdb说的很清楚了吧,对数组名取地址就相当于指向整个数组的一个指针。这样在加减指针运算的时候,步长是整个数组的大小。






在2009-11-03?16:33:21,"刘小林"?<[email protected]>?写道:
>有哪位朋友可以讲讲数组名取地址的问题?在网上看了很多说法,实在是总结不出来个究竟。
>很明显,如今的编译器(只考察了VC++?2008和GCC?3.4.5)是支持数组名取地址的,那么对数组名取地址到底会得到什么?
>
>>

--~--~---------~--~----~------------~-------~--~----~
 要向邮件组发送邮件,请发到 [email protected]
 要退订此邮件,请发邮件至 [email protected]
-~----------~----~----~----~------~----~------~--~---

回复