[고급 C++]포인터 3편(배열 포인터/포인터 배열/문자열 상수 포인터)

 

 

 

[고급 C++]포인터 1편(프로세스/포인터변수/포인터연산/NULL포인터)

프로세스 프로세스 정의 메모리에서 실행중인 프로그램 특징 - xx번지라고 하는 주소 개념을 가짐 - 메모리 주소 공간은 스택 세그먼트 / 힙세그먼트 / 데이터 / 코드 세그먼트로 구분 - 제한된 공

mk28.tistory.com

 

 

[고급 C++]포인터 2편(배열과 포인터)

배열과 포인터 1차원 배열과 포인터  배열 특성 - 배열의 이름은 배열의 시작주소(num = &num[0] =배열의 시작주소) - 연속적인 메모리 할당 보장  1  #include  2  int main()  3  {  4 int num[5] = {1..

mk28.tistory.com

 

 

 

 

배열포인터

다차원 배열의 시작주소를 저장하여 데이터로 사용하는 포인터 변수를 의미

* 2차원 배열포인터 : 2차원 정수형 배열 중에서 열의 크기가 5열인 배열의 시작주소가 대상

* cf) 포인터 변수: 1차원배열의 시작주소이거나 또는 변수의 시작주소가  대상

 

 

자료형 (*변수명)[배열크기];

 

예시

     1  #include <stdio.h>

     2

     3  int main()

     4  {

     5     int a,  b[3],  x1[4][5],  x2[7][3],  x3[2][5];

     6     int *p1;             // 포인터 변수: 1차원배열의 시작주소이거나 또는 변수의 시작주소가  대상

     7     int (*p2)[5];    // 2차원배열포인터 : 2차원 정수형 배열 중에서 열의 크기가 5열인 배열의 시작주소가 대상

     8

     9     printf("%d %d \n", sizeof(p1), sizeof(p2));

    10

    11

    12     p1 = &a;   // o

    13     p1 = b;      // o

    14     p1 = x1;    // x 대상이 잘못됨

    15     p1 = x2;    // x

    16     p1 = x3;     // x

    17

    18     p2 = &a;   // x

    19     p2 = b;     // x

    20     p2 = x1;   // o

    21     p2 = x2;   // x 행크기는 달라도 열크기 같아야함

    22     p2 = x3;   // o

    23     return 0;

    24  }

!cc pointer5.c

"pointer5.c", line 14: warning #2513-D: a value of type "int (*)[5]" cannot be assigned to an entity of type "int *"

        p1 = x1; // x 대상이 잘못됨

           ^

 

"pointer5.c", line 15: warning #2513-D: a value of type "int (*)[3]" cannot be assigned to an entity of type "int *"

        p1 = x2; // x

           ^

 

"pointer5.c", line 16: warning #2513-D: a value of type "int (*)[5]" cannot be assigned to an entity of type "int *"

        p1 = x3; // x

           ^

 

"pointer5.c", line 18: warning #2513-D: a value of type "int *" cannot be   assigned to an entity of type "int (*)[5]"

        p2 = &a; // x

           ^

 

"pointer5.c", line 19: warning #2513-D: a value of type "int *" cannot be assigned to an entity of type "int (*)[5]"

        p2 = b;  // x

           ^

 

"pointer5.c", line 21: warning #2513-D: a value of type "int (*)[3]" cannot be  assigned to an entity of type "int (*)[5]"

        p2 = x2; // x 행크기는 달라도 열크기 같아야함

           ^

4 4

* p2++하면 주소값 20씩(열의 크기 * 대상자료형)만큼 증가!

 

 

포인터배열

포인터 배열은 주소 값들을 저장하는 배열

* 배열포인터가 다차원배열을 가리키며 주소를 1개 갖는 것에 반해, 포인터배열은 포인터변수가 여러개임(주소 4Byte * 개수)

 

선언

자료형 *변수명[배열크기]

- int (*a1)[3];  !=  int *a2[3];

 

 

예시

#include <stdio.h>
int main()
{
	int num[3] = {100, 200, 300}, i;
	int *p1;    // 포인터변수 : 4byte
	int *p2[3]; // 포인터 배열 : 4byte*3

	printf("p1 sizeof : %d \n", sizeof(p1));
	printf("p2 sizeof : %d \n", sizeof(p2));

	return 0;
}

p1 sizeof : 4
p2 sizeof : 12

 

문자열 상수 포인터

문자열 상수를 사용하기 위해 문자열을 메모리의 빈공간에 저장하고 시작주소를 반환하는데 이때 사용하는 것이 문자열 포인터

 

특징

- 저장된 주소가 사용

* “” 문자열상수!

 

예시

#include <stdio.h>

int main()
{	
	char *ptr;
	ptr = "Hello World !!!";

	printf("ptr: %p, %s \n", ptr, ptr);

	return 0;
}

ptr: 040007d0, Hello World !!!

 

 

활용

문자열 상수 포인터를 이용한 문자열 출력

     1  #include <stdio.h>

     2

     3  int main()

     4  {

     5     int i;

     6     char *ptr;

     7

     8     ptr = "Hello World !!!";

     9     printf("ptr: %p, %s \n", ptr, ptr);

    10

    11     char *ptr2[5];

    12     char *ptr1[5]={"kingdom", "Advanced C Program",  "C++ Program", "C#s ", "one two three"};

    14

    15     // 메모리 할당

    16     printf("ptr sizeof : %d %d\n", sizeof(ptr1), sizeof(ptr2));

    17

    18     // 자료형에 따라 주소(%p) , 문자열(%s)

    19     for(i=0; i<5; i++)

    20        printf("%p: %s \n", ptr1[i], ptr1[i]);

    21

    22     // unsized 사이즈 구하기

    23     // NULL로 끝을 알림. 거짓일때까지 반복 가능

    24     char *ptr3[] = {"kingdom", "Advanced c p",  "하이 하이", "호이호이", NULL};

    26     for(i=0; ptr3[i]; i++)

    27        printf("%p %s \n", ptr3[i], ptr3[i]);

    28

    29     return 0;

    30  }

:!a.out

ptr: 040007d0, Hello World !!!

ptr sizeof : 20 20

040007f0: kingdom

04000800: Advanced C Program

04000820: C++ Program

04000830: C#s

04000840: one two three

040007f0 kingdom

04000880 Advanced c p

04000890 하이 하이

040008a0 호이호이

 


 

 

 

 

 

궁금한 사항은 댓글로 남겨주세요💃💨💫
좋아요와 구독(로그인X)은 힘이 됩니다 🙈🙉

 

 

 

반응형
그리드형

댓글

❤️김세인트가 사랑으로 키웁니다❤️