先贴点代码,后面再说明问题.
- #include <stdio.h>
- void god(int [3][2][2]);
- int main(void){
- int i[3][2][2]={{{123,456},{789,101}},{{121,314},{151,617}},{{888,777},{999,333}}};
- int (*p)[2][2]=i;
- god(p);
- return 0;
- }
- void god(int i[3][2][2]){
- printf("%d\n",i[2][0][1]);
- printf("%d\n",*(*(*(i+2))+1));
- printf("%d\n",*(i+2)[0][1]);
- }
- .file "test.c"
- .text
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- andl $-16, %esp
- subl $80, %esp
- movl $123, 28(%esp)
- movl $456, 32(%esp)
- movl $789, 36(%esp)
- movl $101, 40(%esp)
- movl $121, 44(%esp)
- movl $314, 48(%esp)
- movl $151, 52(%esp)
- movl $617, 56(%esp)
- movl $888, 60(%esp)
- movl $777, 64(%esp)
- movl $999, 68(%esp)
- movl $333, 72(%esp)
- leal 28(%esp), %eax
- movl %eax, 76(%esp)
- movl 76(%esp), %eax
- movl %eax, (%esp)
- call god
- movl $0, %eax
- leave
- ret
- .size main, .-main
- .section .rodata
- .LC0:
- .string "%d\n"
- .text
- .globl god
- .type god, @function
- god:
- pushl %ebp
- movl %esp, %ebp
- subl $24, %esp
- movl 8(%ebp), %eax
- addl $32, %eax
- movl 4(%eax), %edx
- movl $.LC0, %eax
- movl %edx, 4(%esp)
- movl %eax, (%esp)
- call printf
- movl 8(%ebp), %eax
- addl $36, %eax
- movl (%eax), %edx
- movl $.LC0, %eax
- movl %edx, 4(%esp)
- movl %eax, (%esp)
- call printf
- movl 8(%ebp), %eax
- addl $40, %eax
- movl (%eax), %edx
- movl $.LC0, %eax
- movl %edx, 4(%esp)
- movl %eax, (%esp)
- call printf
- leave
- ret
- .size god, .-god
- .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
- .section .note.GNU-stack,"",@progbits
- test.s
关键在于对应的3句打印结果的汇编,分别是:
printf("%d\n",i[2][0][1]); 对应着 addl $32, %eax 和 movl 4(%eax), %edx;
printf("%d\n",*(*(*(i+2))+1)); 对应着 addl $36, %eax 和 movl (%eax), %edx;
printf("%d\n",*(i+2)[0][1]); 对应着 movl 8(%ebp), %eax 和 addl $40, %eax;
然后他们打印出来的值顺序为:777 777 999
这说明一个问题,对数组操作,要么就都指针,要么就都下标,尽量避免这种组合型的.
阅读(1150) | 评论(3) | 转发(0) |