Skip to content

Instantly share code, notes, and snippets.

@Rockheung
Created January 12, 2019 06:37
Show Gist options
  • Save Rockheung/c8bd1a067f0469411160f40e2378120a to your computer and use it in GitHub Desktop.
Save Rockheung/c8bd1a067f0469411160f40e2378120a to your computer and use it in GitHub Desktop.

Data Structure Chapter 2 - Pointer

포인터

  • 사용하는 모든 변수는 메모리의 특정 위치에 저장되는데, 그 위치를 나타내는 메모리의 주소를 포인터라고 한다!
/* 포인터 변수의 크기는 일반적으로 메모리 주소의 크기인 2바이트 */
/* 포인터 변수의 타입은 해당 포인터가 가리키는 데이이터 길이를 뜻한다고 볼 수 있다. */
/* 그러니까 액세스 범위를 뜻한다고도 할 수 있다. */
char *ptr_c;
short *ptr_s;
int *ptr_i;
  • 포인터 연산: 주소 연산자(&), 참조 연산자(*)
포인터 = &변수;

*포인터 = ;
변수 = *포인터;
#include <stdio.h>

int main() {

    int i = 10, j = 20;
    int *ptr;

    printf("\n i: %d, j: %d", i, j);
    printf("\n i addr: %u, j addr: %u", &i, &j);

    ptr = &i;
    printf("\n ptr addr: %u, ptr: %u, ptr val: %d", &ptr, ptr, *ptr);

    ptr = &j;
    printf("\n ptr addr: %u, ptr: %u, ptr val: %d", &ptr, ptr, *ptr);

    return 0;
}
$ gcc 2-8.c -o 2-8.out && ./2-8.out
2-8.c: In function ‘main’:
2-8.c:9:25: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("\n i addr: %u, j addr: %u", &i, &j);
                        ~^               ~~
                        %ls
2-8.c:9:37: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int *’ [-Wformat=]
     printf("\n i addr: %u, j addr: %u", &i, &j);
                                    ~^       ~~
                                    %ls
2-8.c:12:27: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int **’ [-Wformat=]
     printf("\n ptr addr: %u, ptr: %u, ptr val: %d", &ptr, ptr, *ptr);
                          ~^                         ~~~~
2-8.c:12:36: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int *’ [-Wformat=]
     printf("\n ptr addr: %u, ptr: %u, ptr val: %d", &ptr, ptr, *ptr);
                                   ~^
                                   %ls
2-8.c:15:27: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int **’ [-Wformat=]
     printf("\n ptr addr: %u, ptr: %u, ptr val: %d", &ptr, ptr, *ptr);
                          ~^                         ~~~~
2-8.c:15:36: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int *’ [-Wformat=]
     printf("\n ptr addr: %u, ptr: %u, ptr val: %d", &ptr, ptr, *ptr);
                                   ~^
                                   %ls

 i: 10, j: 20
 i addr: 2879895480, j addr: 2879895484
 ptr addr: 2879895488, ptr: 2879895480, ptr val: 10
 ptr addr: 2879895488, ptr: 2879895484, ptr val: 20
  • 포인터 초기화
int i;
int *ptr = &i;

char *ptr_c1 = (char *)malloc(100);

char *ptr_c2 = "korea";

char A[100];
char *ptr = A;

char A[100];
char *ptr = &A[0];
  • 예제2-9, 포인터를 이용해 문자열 처리
#include <stdio.h>

int main() {

    int i;
    char str1[20] = "Dreams come true!", str2[20], *ptr1, *ptr2;

    ptr1 = str1;
    printf("\n str1 addr: %u, \t ptr1 addr: %u", str1, ptr1);
    printf("\n str1: %s, \t ptr1: %s", str1, ptr1);
    printf("\n\n ptr1+7 %s", ptr1 + 7);
    ptr2 = &str1[7];
    printf("\n ptr2<=str2[7]: %s\n\n", ptr2);

    for (i=16; i>=0; i--){
        putchar(*(ptr1+i));
    }
    for (i=0; i<20; i++) {
        str2[i] = *(ptr1 + i);
    }
    printf("\n\n str1: %s", str1);
    printf("\n str2: %s", str2);

    *(ptr1+0) = 'P';
    *(ptr1+1) = 'e';
    *(ptr1+2) = 'a';
    *(ptr1+3) = 'c';
    *(ptr1+4) = 'e';
    *(ptr1+5) = 's';
    printf("\n\n str1: %s\n", str1);

    return 0;
}
$ gcc 2-9.c -o 2-9.out && ./2-9.out
2-9.c: In function ‘main’:
2-9.c:9:28: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char *’ [-Wformat=]
     printf("\n str1 addr: %u, \t ptr1 addr: %u", str1, ptr1);
                           ~^                     ~~~~
                           %s
2-9.c:9:46: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘char *’ [-Wformat=]
     printf("\n str1 addr: %u, \t ptr1 addr: %u", str1, ptr1);
                                             ~^
                                             %s

 str1 addr: 2029350048, 	 ptr1 addr: 2029350048
 str1: Dreams come true!, 	 ptr1: Dreams come true!

 ptr1+7 come true!
 ptr2<=str2[7]: come true!

!eurt emoc smaerD

 str1: Dreams come true!
 str2: Dreams come true!

 str1: Peaces come true!
  • 2-10, 포인터 배열을 이용해 문자열 처리
#include <stdio.h>

int main() {

    int i;
    char *ptrArray[4] = {{"Korea"},{"Seoul"},{"Mapo"},{"152번지 2/3"}};

    for (i=0; i<4; i++) {
        printf("\n %s", ptrArray[i]);
    }

    ptrArray[2] = "Jongno";
    printf("\n\n");
    for (i=0; i<4; i++) {
        printf("\n %s", ptrArray[i]);
    }

    return 0;
}
$ gcc 2-10.c -o 2-10.out && ./2-10.out
2-10.c: In function ‘main’:
2-10.c:6:5: warning: braces around scalar initializer
     char *ptrArray[4] = {{"Korea"},{"Seoul"},{"Mapo"},{"152번지 2/3"}};
     ^~~~
2-10.c:6:5: note: (near initialization for ‘ptrArray[0]’)
2-10.c:6:5: warning: braces around scalar initializer
2-10.c:6:5: note: (near initialization for ‘ptrArray[1]’)
2-10.c:6:5: warning: braces around scalar initializer
2-10.c:6:5: note: (near initialization for ‘ptrArray[2]’)
2-10.c:6:5: warning: braces around scalar initializer
2-10.c:6:5: note: (near initialization for ‘ptrArray[3]’)

 Korea
 Seoul
 Mapo
 152번지 2/3


 Korea
 Seoul
 Jongno
 152번지 2/3
  • 2-11, 포인터의 배열과 포인터의 포인터 사용하기
#include <stdio.h>

int main() {

    char *ptrArray[2];
    char **ptrptr;
    int i;

    ptrArray[0] = "Korea";
    ptrArray[1] = "Seoul";

    ptrptr = ptrArray;
    printf("\n ptrArray[0] addr: %u", &ptrArray[0]);
    printf("\n ptrArray[0] has: %u", ptrArray[0]);
    printf("\n ptrArray[0] val: %c", *ptrArray[0]);
    printf("\n ptrArray[0] val_str: %s\n", *ptrArray);

    printf("\n ptrArray[1] addr: %u", &ptrArray[1]);
    printf("\n ptrArray[1] has: %u", ptrArray[1]);
    printf("\n ptrArray[1] val: %c", *ptrArray[1]);
    printf("\n ptrArray[1] val_str: %s\n", *ptrArray);

    printf("\n ptrptr addr: %u", &ptrptr);
    printf("\n ptrptr has: %u", ptrptr);
    printf("\n ptrptr 1st val: %u", *ptrptr);
    printf("\n ptrptr 2nd val: %c", **ptrptr);
    printf("\n ptrptr 2nd val_str: %s", *ptrptr);

    return 0;
}
$ gcc 2-11.c -o 2-11.out && ./2-11.out
> 

2-11.c: In function ‘main’:
2-11.c:15:35: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char **’ [-Wformat=]
     printf("\n ptrArray[0] addr: %u", &ptrArray[0]);
                                  ~^   ~~~~~~~~~~~~
2-11.c:16:34: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char *’ [-Wformat=]
     printf("\n ptrArray[0] has: %u", ptrArray[0]);
                                 ~^   ~~~~~~~~~~~
                                 %s
2-11.c:20:35: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char **’ [-Wformat=]
     printf("\n ptrArray[1] addr: %u", &ptrArray[1]);
                                  ~^   ~~~~~~~~~~~~
2-11.c:21:34: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char *’ [-Wformat=]
     printf("\n ptrArray[1] has: %u", ptrArray[1]);
                                 ~^   ~~~~~~~~~~~
                                 %s
2-11.c:25:30: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char ***’ [-Wformat=]
     printf("\n ptrptr addr: %u", &ptrptr);
                             ~^   ~~~~~~~
2-11.c:26:29: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char **’ [-Wformat=]
     printf("\n ptrptr has: %u", ptrptr);
                            ~^
2-11.c:27:33: warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char *’ [-Wformat=]
     printf("\n ptrptr 1st val: %u", *ptrptr);
                                ~^   ~~~~~~~
                                %s

 ptrArray[0] addr: 312596688
 ptrArray[0] has: 20814036
 ptrArray[0] val: K
 ptrArray[0] val_str: Korea

 ptrArray[1] addr: 312596696
 ptrArray[1] has: 20814042
 ptrArray[1] val: S
 ptrArray[1] val_str: Korea

 ptrptr addr: 312596680
 ptrptr has: 312596688
 ptrptr 1st val: 20814036
 ptrptr 2nd val: K
 ptrptr 2nd val_str: Korea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment