게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
포인터 관련 퍼즐인데 답을 보고도 이해가 안되네요
게시물ID : programmer_15834짧은주소 복사하기
작성자 : 오늩의유머
추천 : 0
조회수 : 606회
댓글수 : 16개
등록시간 : 2016/02/16 08:04:59

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
 
int main()
{  
    int *a[] = {0,1,2,3,4};
 
    printf("arr0=%d\n", *a+0);
    printf("arr1=%d\n", *a+1);
    printf("arr2=%d\n", *a+2);
    printf("arr3=%d\n", *a+3);
    printf("arr4=%d\n", *a+4);
 
    return 0;
}

이렇게 하면 결과가
arr0=0 arr1=4 arr2=8 arr3=12 arr4=16
결과가 이렇게 나온다는데
설명을 봐도 전혀 이해가 안되네요.

기계어로는 
000000000040052d <main>:
  40052d: 55                   push   %rbp
  40052e: 48 89 e5             mov    %rsp,%rbp
  400531: 48 83 ec 30           sub    $0x30,%rsp
  400535: 48 c7 45 d0 00 00 00 movq   $0x0,-0x30(%rbp) # placing array onto stack
  40053c: 00
  40053d: 48 c7 45 d8 00 00 00 movq   $0x0,-0x28(%rbp)
  400544: 00
  400545: 48 c7 45 e0 00 00 00 movq   $0x0,-0x20(%rbp)
  40054c: 00
  40054d: 48 c7 45 e8 00 00 00 movq   $0x0,-0x18(%rbp)
  400554: 00
  400555: 48 c7 45 f0 00 00 00 movq   $0x0,-0x10(%rbp)
  40055c: 00
  40055d: 48 8b 45 d0           mov    -0x30(%rbp),%rax # move first array value to rax register
  400561: 48 89 c6             mov    %rax,%rsi # no add just move rax value
  400564: bf 94 06 40 00       mov    $0x400694,%edi
  400569: b8 00 00 00 00       mov    $0x0,%eax
  40056e: e8 9d fe ff ff       callq  400410 <printf@plt>
  400573: 48 8b 45 d0           mov    -0x30(%rbp),%rax # move first array value to rax register
  400577: 48 83 c0 04           add    $0x4,%rax # add 4 to the value in the rax register
  40057b: 48 89 c6             mov    %rax,%rsi
  40057e: bf 9d 06 40 00       mov    $0x40069d,%edi
  400583: b8 00 00 00 00       mov    $0x0,%eax
  400588: e8 83 fe ff ff       callq  400410 <printf@plt>
  40058d: 48 8b 45 d0           mov    -0x30(%rbp),%rax # move first array value to rax register
  400591: 48 83 c0 08           add    $0x8,%rax # add 8 to the value in the rax register
  400595: 48 89 c6             mov    %rax,%rsi
  400598: bf a6 06 40 00       mov    $0x4006a6,%edi
  40059d: b8 00 00 00 00       mov    $0x0,%eax
  4005a2: e8 69 fe ff ff       callq  400410 <printf@plt>
  4005a7: 48 8b 45 d0           mov    -0x30(%rbp),%rax # move first array value to rax register
  4005ab: 48 83 c0 0c           add    $0xc,%rax # add 12 to the value in the rax register
  4005af: 48 89 c6             mov    %rax,%rsi
  4005b2: bf af 06 40 00       mov    $0x4006af,%edi
  4005b7: b8 00 00 00 00       mov    $0x0,%eax
  4005bc: e8 4f fe ff ff       callq  400410 <printf@plt>
  4005c1: 48 8b 45 d0           mov    -0x30(%rbp),%rax # move first array value to rax register
  4005c5: 48 83 c0 10           add    $0x10,%rax # add 16 to the value in the rax register
  4005c9: 48 89 c6             mov    %rax,%rsi
  4005cc: bf b8 06 40 00       mov    $0x4006b8,%edi
  4005d1: b8 00 00 00 00       mov    $0x0,%eax

이렇다는데 a가 포인터의 배열이라는 것도 알겠고, 
a에 값이 0, 1, 2, 3, 4가 들어간다는 말도 알겠는데
어떻게 저런 결과가 나오는지는 이해가 안되는군요.

혹시 이 퍼즐이 이해가 되시는 능력자분이 있을까요?



출처 http://denniskubes.com/2014/08/11/interesting-pointer-puzzle/
꼬릿말 보기
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호