포인터 풀이 죽은포인터 업 캐스팅과 다운 캐스트
int* ptrInt;
char * ptrChar;
void* ptrVoid;
unsigned char indx;
int sample = 0x12345678;
ptrInt = &sample;
ptrVoid = (void *)(ptrInt);
ptrChar = (char *)(ptrVoid);
/*manipulating ptrChar */
for (indx = 0; indx < 4; indx++)
{
printf ("\n Value: %x \t Address: %p", *(ptrChar + indx), (ptrChar + indx));
}
출력 :
Value: 00000078 Address: 0022FF74
Value: 00000056 Address: 0022FF75
Value: 00000034 Address: 0022FF76
Value: 00000012 Address: 0022FF77
질문 : 왜 샘플 문자 크기의 데이터로 분할되었다? 그리고 포인터 연산이 수행 될 때 어떻게 나머지 값을 얻을 수 있었습니까? 어떻게 가능 했습니까?
포인터 업 캐스팅
unsigned int * ptrUint;
void * ptrVoid;
unsigned char sample = 0x08;
ptrVoid = (void *)&sample;
ptrUint = (unsigned int *) ptrVoid;
printf(" \n &sample: %p \t ptrUint: %p ", &sample, ptrUint);
printf(" \n sample: %p \t *ptrUint: %p ", sample, *ptrUint);
출력 :
&sample: 0022FF6F ptrUint: 0022FF6F
sample: 00000008 *ptrUint: 22FF6F08 <- Problem Point
질문 : 왜 * ptrUint에 쓰레기 값이 있다는 것입니다? 왜 쓸데없는 값이 비슷합니까? to ptrUint? 이 쓰레기 값을 피하기 위해 malloc() 또는 calloc()을 사용해야합니까? 쓰레기 값을 제거하기 위해 어떤 종류의 해결책을 제안 하시겠습니까?
미안하지만 질문에 너무 집중해야합니다. 올바른 형식을 잊어 버렸습니다. 사과. –