<div><span style="font-size: 9pt; line-height: 1.5">#include stdio.h<stdio.h></stdio.h></span></div> <div><span style="font-size: 9pt; line-height: 1.5">#include conio.h <conio.h>// _getch(); 를 사용하기 위하여</conio.h></span></div> <div><span style="font-size: 9pt; line-height: 1.5"><br /></span></div> <div><span style="font-size: 9pt; line-height: 1.5">/*</span></div> <div><span style="font-size: 9pt; line-height: 1.5"> a라는 변수를 선언하면 프로그램은 메모리의 4바이트 공간을 a라는 이름으로 사용합니다.</span></div> <div><span style="font-size: 9pt; line-height: 1.5"> 전역변수의 경우 미리 정해놓은 영역, </span><span style="font-size: 9pt; line-height: 1.5">지역변수의 경우 함수 또는 해당 블럭이 실행되는 동안</span></div> <div><span style="font-size: 9pt; line-height: 1.5"> 스택에 잠시 할당받은 공간을 이용하지만 </span><span style="font-size: 9pt; line-height: 1.5">모든 것은 중요하지 않습니다.</span></div> <div><span style="font-size: 9pt; line-height: 1.5"> 결</span><span style="font-size: 9pt; line-height: 1.5">국 <font color="#ff0000">a는 메모리의 공간</font>이기 때문입니다.</span></div> <div><span style="font-size: 9pt; line-height: 1.5"><br /></span></div> <div> <div> 본문에 사용되는 LPBYTE 는 BYTE *, <span style="font-size: 9pt; line-height: 1.5">unsigned char * 와 같습니다.</span></div> <div><span style="font-size: 9pt; line-height: 1.5"> (LPBYTE) 는 (BYTE *) 또는 (unsigned char *) 로 바꾸셔도 된다는 말입니다.</span></div></div> <div>*/</div> <div><br /></div> <div><span style="font-size: 9pt; line-height: 1.5">int main(int argc, char *argv[])</span></div> <div>{</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>// a라는 공간 안에 1 이라는 값을 대입합니다.</div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span>int a = 1;</span></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>// 잘 들어갔는지 확인해봅니다.</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>printf("%d\n", a);</div> <div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>/*</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span> 여기서 포인터를 사용하기 위해서 알아두셔야할 부분이 있습니다.</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span> 컴퓨터는 메모리에 값을 거꾸로 저장합니다.</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span> int는 4바이트지만 1을 저장하면 <span style="font-size: 9pt; line-height: 1.5">즉 00 00 00 01 이지만 </span><span style="font-size: 9pt; line-height: 1.5">01, 00, 00, 00 으로 저장이 됩니다.</span></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>*/</div></div> <div><br /></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>// int 는 4바이트라서 +1 을 하면 주소를 4바이트만큼 더합니다.</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>printf("%x %x %x %x\n", &a, ((LPBYTE)(&a)+1), ((LPBYTE)(&a)+2), ((LPBYTE)(&a)+3));</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>// 변수의 값을 1바이트씩 확인해봅시다. <span style="font-size: 9pt; line-height: 1.5"> a의 4바이트를 1바이트씩 보려면 </span><span style="font-size: 9pt; line-height: 1.5">&(a) 의 앞에 (unsigned char *) 를 붙여야합니다</span></div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span>// 그래야 주소가 1바이트씩 더해집니다. ( 주소의 더하기는 변수의 형태를 따릅니다, int는 4바이트라서 4씩 더해집니다 )</span></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>printf("%2d %2d %2d %2d\n", *(LPBYTE)(&a), *(LPBYTE)((LPBYTE)(&a)+1), *(LPBYTE)((LPBYTE)(&a)+2), *(LPBYTE)((LPBYTE)(&a)+3));</div> <div><br /></div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span>// </span><span style="font-size: 9pt; line-height: 1.5">*는 값, &는 주소입니다.</span></div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span><font color="#ff0000">*(int *)(&a) = 2;</font></span></div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span>/*</span></div> <div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>이것을 직역하면 아래와 같습니다.</div> <div><span class="Apple-tab-span" style="font-size: 9pt; line-height: 1.5; white-space: pre"> </span><span style="font-size: 9pt; line-height: 1.5"><font color="#ff0000"><span style="background-color: #ffffff">값을수정하자(int 형태의 값)(a의 주소) = 2</span>;</font><span class="Apple-tab-span" style="white-space: pre"> </span><==========</span></div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span></span><span style="font-size: 9pt; line-height: 1.5"><font color="#0070c0">a주소의 값을 2로 수정하자</font></span></div> <div><span class="Apple-tab-span" style="font-size: 9pt; line-height: 1.5; white-space: pre"> </span><span style="font-size: 9pt; line-height: 1.5">int는 4바이트 입니다. int 형태의 값 이라는것은 4바이트만큼을 수정하겠다는 의미입니다.</span></div></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>확인해봅시다.</div> <div><span class="Apple-tab-span" style="font-size: 9pt; line-height: 1.5; white-space: pre"> </span><span style="font-size: 9pt; line-height: 1.5">*/</span></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>printf("%x %x %x %x\n", &a, ((LPBYTE)(&a)+1), ((LPBYTE)(&a)+2), ((LPBYTE)(&a)+3));</div> <div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>printf("%2d %2d %2d %2d\n", *(LPBYTE)(&a), *(LPBYTE)((LPBYTE)(&a)+1), *(LPBYTE)((LPBYTE)(&a)+2), *(LPBYTE)((LPBYTE)(&a)+3));</div></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>/*</div> <div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span></span><span style="font-size: 9pt; line-height: 1.5">응용하면 *(unsigned char *)(&a) = 3; 로 사용할수도 있습니다.</span></div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span>주소 a의 값을 1바이트만큼 3으로 바꾸겠다는 내용입니다.</span></div></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>확인해봅시다.</div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span>*/</span></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span><span style="font-size: 9pt; line-height: 1.5">*(</span>LPBYTE<span style="font-size: 9pt; line-height: 1.5">)(&a) = 3;</span></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span><span style="font-size: 9pt; line-height: 1.5">printf("%x %x %x %x\n", &a, ((LPBYTE)(&a)+1), ((LPBYTE)(&a)+2), ((LPBYTE)(&a)+3));</span></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>printf("%2d %2d %2d %2d\n", *(LPBYTE)(&a), *(LPBYTE)((LPBYTE)(&a)+1), *(LPBYTE)((LPBYTE)(&a)+2), *(LPBYTE)((LPBYTE)(&a)+3));</div> <div><br /></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>/*</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>이번에는 a 주소에 +1한 주소의 값을 1바이트만큼 4로 수정해봅시다.</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>그리고 결과를 확인합니다. 아마 잘 수정되었을겁니다</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>*/</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span><span style="font-size: 9pt; line-height: 1.5">*(</span>LPBYTE<span style="font-size: 9pt; line-height: 1.5">)((LPBYTE)(&a)+1) = 4;</span></div> <div> <div><span style="font-size: 9pt; line-height: 1.5"><span class="Apple-tab-span" style="white-space: pre"> </span>printf("%x %x %x %x\n", &a, ((LPBYTE)(&a)+1), ((LPBYTE)(&a)+2), ((LPBYTE)(&a)+3));</span></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>printf("%2d %2d %2d %2d\n", *(LPBYTE)(&a), *(LPBYTE)((LPBYTE)(&a)+1), *(LPBYTE)((LPBYTE)(&a)+2), *(LPBYTE)((LPBYTE)(&a)+3));</div></div> <div><br /></div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>_getch();</div> <div><span class="Apple-tab-span" style="white-space: pre"> </span>return 0;</div> <div>}</div> <div><br /></div> <div><br /></div> <div>여기에 코딩하며 컴파일은 해보지 않았습니다 에러 있으면 알려주시고 고칠 부분이 있다거나</div> <div>설명이 필요한 부분이 있으면 말씀해주세요</div> <div>이것을 이해하고 응용할수 있다면 1차원적 포인터는 마스터입니다.</div> <div><br /></div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.