게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
C언어 중수를 위한 포인터 예제
게시물ID : programmer_938짧은주소 복사하기
작성자 : 잠이오네요
추천 : 2
조회수 : 1315회
댓글수 : 4개
등록시간 : 2014/02/01 01:12:44
컴파일 환경
x86_64-linux-gnu
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)


코드
#include <stdio.h> 
#include <string.h> 

int main() 

        { 
                char *str1 = "hello!"
                char *str2; 

                printf("str1(%d): %s\\n", strlen(str1), str1); 

                str2 = str1; 
                printf("str2(%d): %s\\n", strlen(str2), str2); 

                printf("please input a character -> "); 
                // *str2 = getchar(); // Segmentation fault here 
                printf("modified str2(%d): %s\\n", strlen(str2), str2); 
        }printf("\\n"); 

        { 
                int a = 65
                char *b = &"ABC"
                int *c = &"ABC"

                printf("a=0x%x, *b=0x%x, *c=0x%x\\n", a, *b, *c); 
                printf("&a=0x%x, b=0x%x, c=0x%x\\n", &a, b, c); 
                printf("&\\"ABC\\" is 0x%x\\n", &"ABC"); 
                printf("int value of \\"ABC\\" is 0x%x\\n", *((int *)&"ABC")); 
        }printf("\\n"); 

        { 
                typedef struct 
                { 
                        char a; 
                        char b; 
                        int c; 
                } user_unpacked_type; 

                #pragma pack(push, 1); 
                typedef struct 
                { 
                        char a; 
                        char b; 
                        int c; 
                } user_type; 
                #pragma pop(); 

                printf("size of user_unpacked_type is %d\\n"sizeof(user_unpacked_type));
                printf("size of user_type is %d\\n"sizeof(user_type)); 

                { 
                        int i; 
                        user_type ut = {'A''B', *((int *)&"CDE")}; 
                        for(i=0; i<sizeof(user_type); i++) 
                                printf("ut[%d] is 0x%x\\n", i, ((char *)&ut)[i]); 
                        printf("or otherwise....\n"); 
                        for(i=0; i<sizeof(user_type)-sizeof(int); i++) 
                                printf("ut[%d] is 0x%x\\n", i, ((int *)&ut)[i]); 
                } 
        }printf("\\n"); 

        { 
                unsigned int a = 1234567890
                int b = *((int *)&a); 
                char c = *((char *)&a); 
                long d = *((long *)&a); 
                printf("a = 0x%x\\n", a); 
                printf("b = 0x%x\\n", b); 
                printf("c = 0x%x\\n", c); 
                printf("d = 0x%x\\n", d); 
        }printf("\\n"); 

        return 0



결과:
// first running
str1(6): hello!
str2(6): hello!
please input a character -> modified str2(6): hello!

a=0x41, *b=0x41, *c=0x434241
&a=0x1ae753b0, b=0x4009c7, c=0x4009c7
&"ABC" is 0x4009c7
int value of "ABC" is 0x434241

size of user_unpacked_type is 8
size of user_type is 6
ut[0] is 0x41
ut[1] is 0x42
ut[2] is 0x43
ut[3] is 0x44
ut[4] is 0x45
ut[5] is 0x0
or otherwise....
ut[0] is 0x44434241
ut[1] is 0x45

a = 0x499602d2
b = 0x499602d2
c = 0xffffffd2
d = 0x499602d2


// second running
str1(6): hello!
str2(6): hello!
please input a character -> modified str2(6): hello!

a=0x41, *b=0x41, *c=0x434241
&a=0xd94056b0, b=0x4009c7, c=0x4009c7
&"ABC" is 0x4009c7
int value of "ABC" is 0x434241

size of user_unpacked_type is 8
size of user_type is 6
ut[0] is 0x41
ut[1] is 0x42
ut[2] is 0x43
ut[3] is 0x44
ut[4] is 0x45
ut[5] is 0x0
or otherwise....
ut[0] is 0x44434241
ut[1] is 0x45

a = 0x499602d2
b = 0x499602d2
c = 0xffffffd2
d = 0x499602d2





코드와 실행 결과가 직관적으로 이해가신다면 당신은 C산맥 중 포인터 고개를 반정도 넘으신겁니다...

함수포인터를 다루는 부분이나 구조체간 타입 캐스팅 같은 것들이 아직 남았...ㅠ

근데 그거 끝내면 또 매크로와 레지스터 접근, CPU캐시와 메모리간의 일관성 관리 이슈가 또 있고

하드웨어 깊숙한 곳까지 하드하게 하면 어셈블리 혼성 코딩이 또 있네요(....)
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호