Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ Q/A
[956] 포인터 size는 4바이트 고정이죠?
오제호 [ohjeho] 1672 읽음    2002-08-05 15:03
다음과 같은 배열의 각 요소를 출력할때

#include <stdio.h>
#include <conio.h>

int main(void)
{
   char a[10]="abcdefghij";
   char b[4]="opqr";
   char *ptra, *ptrb;
   int i;

   ptra=&a[0];
   ptrb=&b[0];

  
   for(i=0;i<=15;i++)
     printf("Address of ptr a : %p \t a[%d] : %c\n",(ptra+i),i,a[i]);
   for(i=0;i<=10;i++)
     printf("Address of ptr b : %p \t b[%d] : %c\n",(ptrb+i),i,b[i]);

   getch();
}

<실행결과>
Address of ptr a : 0012FF80      a[0] : a
Address of ptr a : 0012FF81      a[1] : b
Address of ptr a : 0012FF82      a[2] : c
Address of ptr a : 0012FF83      a[3] : d
Address of ptr a : 0012FF84      a[4] : e
Address of ptr a : 0012FF85      a[5] : f
Address of ptr a : 0012FF86      a[6] : g
Address of ptr a : 0012FF87      a[7] : h
Address of ptr a : 0012FF88      a[8] : i
Address of ptr a : 0012FF89      a[9] : j
Address of ptr a : 0012FF8A      a[10] :
Address of ptr a : 0012FF8B      a[11] :
Address of ptr a : 0012FF8C      a[12] : ?
Address of ptr a : 0012FF8D      a[13] :
Address of ptr a : 0012FF8E      a[14] :
Address of ptr a : 0012FF8F      a[15] : 
Address of ptr b : 0012FF7C      b[0] : o
Address of ptr b : 0012FF7D      b[1] : p
Address of ptr b : 0012FF7E      b[2] : q
Address of ptr b : 0012FF7F      b[3] : r
Address of ptr b : 0012FF80      b[4] : a
Address of ptr b : 0012FF81      b[5] : b
Address of ptr b : 0012FF82      b[6] : c
Address of ptr b : 0012FF83      b[7] : d
Address of ptr b : 0012FF84      b[8] : e
Address of ptr b : 0012FF85      b[9] : f
Address of ptr b : 0012FF86      b[10] : g

결과를 보면 포인터의 크기는 4바이트입니다.(32bit compiler에서)
메모리상 b[0]의 위치는 7C인데 배열 b의 크기는 4바이트이므로
다음번 a[0]는 80부터 위치합니다.
여기서 만약 배열 b의 크기가 5바이트라면
어드레스 80에 b[4]의 값이 저장되어 있으므로
a[0]의 어드레스는 84가 됩니다.
즉 포인터가 가리키는 어드레스는 0,4,8,C에서 시작됩니다.
하지만 아래는 다릅니다.

만약에 a,b를 배열로 초기화하지 않고 포인터로 선언해서 실행시킨 결과는 다음과 같습니다.
Address of ptr a : 004020A4     a[0] : a
Address of ptr a : 004020A5     a[1] : b
Address of ptr a : 004020A6     a[2] : c
Address of ptr a : 004020A7     a[3] : d
Address of ptr a : 004020A8     a[4] : e
Address of ptr a : 004020A9     a[5] : f
Address of ptr a : 004020AA     a[6] : g
Address of ptr a : 004020AB     a[7] : h
Address of ptr a : 004020AC     a[8] : i
Address of ptr a : 004020AD     a[9] : j
Address of ptr a : 004020AE     a[10] :
Address of ptr a : 004020AF     a[11] : o   
Address of ptr a : 004020B0     a[12] : p
Address of ptr a : 004020B1     a[13] : q
Address of ptr a : 004020B2     a[14] :
Address of ptr a : 004020B3     a[15] : p
Address of ptr a : 004020AF     b[0] : o
Address of ptr a : 004020B0     b[1] : p
Address of ptr a : 004020B1     b[2] : q
Address of ptr a : 004020B2     b[3] :
Address of ptr a : 004020B3     b[4] :  p
결과를 보면 어드레스 AE에 a[10]에 해당되는 널값이 출력되고
b[0]값은 어드레스 AF에 출력되었습니다.
제 생각에 포인터크기가 4바이트라면
b[0]값은 첫번째 배열로 선언한것처럼 어드레스가 0,4,8,C로 끝나는 위치에
와야 된다고 생각하는데 결과는 그렇지 않습니다.
a의 모든 요소를 출력하고 널값 출력한 바로 다음 어드레스가
b의 시작 주소가 됩니다.(마치 포인터의 크기가 두바이트 같거든요)

어떤 문자열을 배열로 선언한 것과 포인터로 선언했을 경우
메모리상에 실제 데이타가 위치하는 것이 다릅니다.
포인터 크기는 4바이트로 고정되어야 하는데
뭔가 다르게 동작합니다.

제가 한 질문을 이해하시겠죠?
요점만 물어야 하는데 너무 무식해서 조리있게 질문을 할수가 없네요.
너무 장황하게 설명한것 같네요.

고수님의 답변이나 조언 좀 부탁드리겠습니다.











+ -

관련 글 리스트
956 포인터 size는 4바이트 고정이죠? 오제호 1672 2002/08/05
3474     Re:포인터 size는 4바이트 고정이죠? 방태윤 1724 2002/08/05
3473     Re:포인터 size는 4바이트 고정이죠? 방태윤 1697 2002/08/05
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.