์ƒˆ์†Œ์‹

๐Ÿ‘ฉ‍๐Ÿ’ป C

C ์–ธ์–ด - ํฌ์ธํ„ฐ ์ด์ •๋ฆฌ (Pointer) | void , ์ด์ค‘ ํฌ์ธํ„ฐ , ์—ญ์ฐธ์กฐ

  • -

*ํฌ์ธํ„ฐ (pointer)

 

C์—์„œ ๊ฐ’์„ ์ €์žฅํ• ๋•Œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋•Œ ์ด ๋ณ€์ˆ˜๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ๋˜๋Š”๋ฐ, ์ด ๋ฉ”๋ชจ๋ฆฌ์— ํŠน์ • ์žฅ์†Œ์— ์ €์žฅ๊ณต๊ฐ„์„ ํ™•๋ณดํ•ด๋†“๊ณ  ์ €์žฅ, ๋กœ๋“œ๋ฅผ ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

๋”ฐ๋ผ์„œ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ• ๋•Œ๋Š”, 1. ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•. 2. ๋ฉ”๋ชจ๋ฆฌ์˜ ํŠน์ • ์žฅ์†Œ๊ฐ’(๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ)๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

์ด ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ–๋Š”๊ฒƒ์„ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ๋ผ๊ณ ํ•œ๋‹ค. 

 

*ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

ํฌ์ธํ„ฐ๋Š” ์ปดํ“จํ„ฐ ์‹œ์Šคํ…œ์˜ ํŠน์ • ๋ฉ”๋ชจ๋ฆฌ์— ๋ฐ”๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ตฌ์กฐํ™”๋œ ์ž๋ฃŒ๋ฅผ ๋งŒ๋“ค์–ด ํšจ์œจ์ ์ธ ์šด์˜์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ฐฐ์—ด๊ณผ ๊ตฌ์กฐ์ฒด๋“ฑ์˜ ์ž๋ฃŒ ๊ตฌ์กฐํ•จ์ˆ˜์— ์ ‘๊ทผ์ด ์šฉ์ดํ•˜๋‹ค.

 

 

* ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ | ์ฃผ์†Œ์—ฐ์‚ฐ์ž & | ์„œ์‹์ง€์ •์ž %p

64๋น„ํŠธ ์ปดํ“จํ„ฐ์—์„œ๋Š” 16์ง„์ˆ˜ 16์ž๋ฆฌ์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง„๋‹ค.

  • 0x0000000000000000 ~ 0xFFFFFFFFFFFFFFFF

๊ทธ๋ฆฌ๊ณ  C์—์„œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ฃผ์†Œ์—ฐ์‚ฐ์ž & ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ถœ๋ ฅํ• ๋•Œ ์„œ์‹ ์ง€์ •์ž๋Š” %p์ด๋‹ค.

#include <stdio.h>

int main(){
	int num1 = 2;
    printf("%p", &num1);
}

์ถœ๋ ฅ >>>>>> 0061FF1C

์ด๋•Œ ์ถœ๋ ฅ๋œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ’์€, ์‹คํ–‰ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ, ์‚ฌ์šฉ ์ปดํ“จํ„ฐ์— ๋”ฐ๋ผ ๊ณ„์†ํ•ด์„œ ๋‹ฌ๋ผ์ง„๋‹ค. 

 

*ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ์„ ์–ธ | *(Asterisk, ์• ์Šคํ„ฐ๋ฆฌ์Šคํฌ)

 

ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋ ค๋ฉด *(Asterisk, ์• ์Šคํ„ฐ๋ฆฌ์Šคํฌ) ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. " *๋Š” ์ด ๋ณ€์ˆ˜๊ฐ€ ํฌ์ธํ„ฐ๋‹ค" ๋ผ๊ณ  ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

ํฌ์ธํ„ฐ ์„ ์–ธํ•˜๊ธฐ
int num1 = 12;
int *num1_pointer = &num1;
printf("%p", num1_pointer);

num1_pointer :  num1์˜ ์ฃผ์†Œ๊ฐ’์ด ๋“ค์–ด์žˆ๋Š” ํฌ์ธํŠธ ๋ณ€์ˆ˜์ด๋‹ค.

 

 

num1_pointer๊ฐ€ ๊ฐ–๊ณ ์žˆ๋Š” ์ฃผ์†Œ๊ฐ’์— ๋ญ๊ฐ€ ๋“ค์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด ๋‹ค์‹œ *๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” *๋Š” ์—ญ์ฐธ์กฐ ์—ฐ์‚ฐ์ž(๊ฐ„์ ‘ ์ฐธ์กฐ ์—ฐ์‚ฐ์ž)๋ผ๊ณ ํ•˜๋ฉฐ, ์„ ์–ธํ• ๋•Œ ์‚ฌ์šฉ๋˜๋Š” *์™€๋Š” ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ–๋Š”๋‹ค.

์—ฌ๊ธฐ์„œ์˜ " *๋Š” ์—ญ์ฐธ์กฐํ•˜์—ฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฒ ๋‹ค"๋ผ๋Š” ๋œป์„ ๊ฐ€์ง„๋‹ค.

ํฌ์ธํ„ฐ๋Š” ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ญ์ฐธ์กฐ๋Š” ๊ทธ ์ฃผ์†Œ์— ์ ‘๊ทผํ•˜์—ฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

 

ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ณ€์ˆ˜์˜ ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ
#include <stdio.h>

int main(){
	int num1 = 2;
    int *pointer = &num1;
    printf("%d\n", *pointer);
}

์ถœ๋ ฅ >>>>> 2

 

 

ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ณ€์ˆ˜์˜ ๊ฐ’ ๋ฐ”๊พธ๊ธฐ
#include <stdio.h>
int main(){
    int num1 = 2;
    int *num1_pointer = &num1;
    *num1_pointer = 2000;
    printf("%d", num1);
}

์—ญ์ฐธ์กฐ ์—ฐ์‚ฐ์ž๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— ์ ‘๊ทผํ•˜์—ฌ, 2๋ฅผ 2000์œผ๋กœ ๋ฐ”๊พธ์—ˆ๋‹ค.

 

* ์ž๋ฃŒํ˜•๊ณผ ํฌ์ธํ„ฐ   |  ์ž๋ฃŒํ˜•์ด ์ •ํ•ด์ง€์ง€ ์•Š์€ void ํฌ์ธํ„ฐ

  • C ์— ์žˆ๋Š” ๋ชจ๋“  ์ž๋ฃŒํ˜•์€ ํฌ์ธํ„ฐ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ex ) long long *num1_pointer;     float *num2_pointer;
  • ์„ ์–ธํ•˜๋Š” ์ž๋ฃŒํ˜•์— ๋”ฐ๋ผ ๋ฉ”๋ชจ๋ฆฌ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ค. ์™œ๋ƒ๋ฉด ์ž๋ฃŒํ˜•๋งˆ๋‹ค size๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์—  (int 4๋ฐ”์ดํŠธ, char 1๋ฐ”์ดํŠธ...)
  • ์ž๋ฃŒํ˜•์ด ์ •ํ•ด์ง€์ง€ ์•Š์€ ํฌ์ธํ„ฐ๋„ ์žˆ๋‹ค. ์ด๋ฅผ void ํฌ์ธํ„ฐ๋ผ๊ณ  ํ•œ๋‹ค.
#include <stdio.h>
int main(){
	int num1= 10;
    char c1 = 'a';
    int *num1_Ptr = &num1;    // intํ˜• ํฌ์ธํ„ฐ
    char *c1_Ptr = &c1;       // charํ˜• ํฌ์ธํ„ฐ
    
    void *ptr;              // ์ž๋ฃŒํ˜•์ด ์—†๋Š” void ํฌ์ธํ„ฐ 
  
   // ์ž๋ฃŒํ˜•์ด ์ •ํ•ด์ง€์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ์–ด๋– ํ•œ ์ž๋ฃŒํ˜•์˜ ์ฃผ์†Œ๋ฅผ ๋„ฃ์–ด๋„ ์ƒ๊ด€์—†๋‹ค. 
   ptr = num1_Ptr;
   ptr = c1_Ptr;
   
   // ๋˜ํ•œ ์ž๋ฃŒํ˜•์ด ์ •ํ•ด์ง„ ํฌ์ธํ„ฐ์— void ํฌ์ธํ„ฐ๋ฅผ ์ €์žฅํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.
   num1_Ptr = ptr;
   c1_Ptr = ptr;
   
   return 0; 
}

void ํฌ์ธํ„ฐ๋Š” ์ž๋ฃŒํ˜•์ด ์ •ํ•ด์ง€์ง€ ์•Š์•˜๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์˜ ํŠน์ง•์ด ์žˆ๋‹ค.

 1) ์–ด๋–ค ์ž๋ฃŒํ˜•์œผ๋กœ ๋œ ํฌ์ธํ„ฐ๋„ voidํฌ์ธํ„ฐ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. 

2) voidํฌ์ธํ„ฐ๋ฅผ ์–ด๋–ค ์ž๋ฃŒํ˜•์œผ๋กœ ๋œ ํฌ์ธํ„ฐ์—๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

3) void ํฌ์ธํ„ฐ๋Š” ์—ญ์ฐธ์กฐ๋ฅผ ํ•  ์ˆ˜ ์—†๋‹ค. (์ž๋ฃŒํ˜•์ด ์—†๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜, ์ €์žฅํ•  size๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—)

์—ญ์ฐธ์กฐ๊ฐ€ ๋˜์ง€ ์•Š์Œ์—๋„ void ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” , ํ•จ์ˆ˜์—์„œ ์—ฌ๋Ÿฌ์ข…๋ฅ˜์˜ ์ž๋ฃŒํ˜•์„ ๋ฐ›์•„๋“ค์ผ๋•Œ , ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ํฌ์ธํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ์ข…๋ฅ˜์˜ ์ž๋ฃŒํ˜•์œผ๋กœ ๋œ ํฌ์ธํ„ฐ์— ์ €์žฅํ• ๋•Œ, ์ž๋ฃŒํ˜•์„ ์ˆจ๊ธฐ๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

 

 

*์ƒ์ˆ˜์™€ ํฌ์ธํ„ฐ

ํฌ์ธํ„ฐ์—๋„ const๋ฅผ ๋ถ™์—ฌ ์ƒ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋“ค์ˆ˜์žˆ๋‹ค. ์ด๋•Œ const์˜ ์œ„์น˜์— ๋”ฐ๋ผ์„œ ํŠน์„ฑ์ด ๋‹ฌ๋ผ์ง„๋‹ค.

1. ์ƒ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ (pointer to constant)
const int num1 = 10;
const int *num1_pointer = &num1;

*num_pointer = 1300;        >>>>>> ์ปดํŒŒ์ผ ์—๋Ÿฌ

๋งˆ์ง€๋ง‰ ์ค„์—์„œ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ด์œ  : ๋ณ€์ˆ˜ num1์ด ์ƒ์ˆ˜์ธ๋ฐ ๊ฐ’์„ ๋ฐ”๊พธ๋ ค ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—.

 

2. ํฌ์ธํ„ฐ ์ž์ฒด๊ฐ€ ์ƒ์ˆ˜
int num1 = 10;
int num2 = 1;
int * const num1_pointer = &num1;

num1_pointer = &num2;   >>>>>>์ปดํŒŒ์ผ ์—๋Ÿฌ

๋งˆ์ง€๋ง‰ ์ค„์—์„œ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ด์œ  : ํฌ์ธํ„ฐ ์ž์ฒด๊ฐ€ ์ƒ์ˆ˜์ธ๋ฐ, ๋‹ค๋ฅธ ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๊ฐ’์„ ๋„ฃ์œผ๋ ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—

 

3. ์ƒ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉด์„œ, ํฌ์ธํ„ฐ ์ž์ฒด๋„ ์ƒ์ˆ˜
const int num1 = 10;
const int * const num1_pointer = &num1;

1๋ฒˆ๊ฒฝ์šฐ + 2๋ฒˆ๊ฒฝ์šฐ ์ด๊ธฐ ๋•Œ๋ฌธ์—, ํฌ์ธํ„ฐ์˜ ์ฃผ์†Œ๊ฐ’๋„ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ , ์—ญ์ฐธ์กฐํ•˜์—ฌ ๋ณ€์ˆ˜๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š”๊ฒƒ๋„ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

* ํฌ์ธํ„ฐ ์ฝ๋Š” ๋ฐฉ๋ฒ•

๋ฌด์กฐ๊ฑด ์—ญ์ˆœ์œผ๋กœ ์ฝ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

ํฌ์ธํ„ฐ ์ฝ๋Š” ๋ฐฉ๋ฒ•

 

 

* ์ด์ค‘ ํฌ์ธํ„ฐ ( ํฌ์ธํ„ฐ์˜ ํฌ์ธํ„ฐ)

 

  • ํฌ์ธํ„ฐ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ (์ด์ค‘ ํฌ์ธํ„ฐ)๋Š” *๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ๋‹ค.
  • ์—ญ์ฐธ์กฐ๋•Œ๋„ *๋ฅผ ๋‘๋ฒˆ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
  • ํฌ์ธํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด์ค‘ ํฌ์ธํ„ฐ์—๋งŒ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค.
  • ์ด์ค‘ ํฌ์ธํ„ฐ๋ฟ ์•„๋‹ˆ๋ผ, 3์ค‘ 4์ค‘ ๊ทธ ์ด์ƒ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
#include <stdio.h>
int main(){
	int *pointer1;
    int **pointer2;
    int num = 10;
    
    pointer1 = &num;
    pointer2 = &pointer1;

    printf("%d\n", num);
    printf("%d\n", *pointer1);
    printf("%d\n", **pointer2);
    return 0;
}

์ถœ๋ ฅ >>>>> 10 10 10

num์˜ ์ฃผ์†Œ๊ฐ’์„ pointer1๊ฐ€ ๊ฐ€์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  pointer1์˜ ์ฃผ์†Œ๊ฐ’์„ pointer2๊ฐ€ ๊ฐ€์ง„๋‹ค. 

pointer2์˜ ์ฐธ์กฐ ๊ฒฝ๋กœ : pointer1์˜ ์ฃผ์†Œ๊ฐ’-> num1์˜ ์ฃผ์†Œ๊ฐ’ -> num1 (๊ฐ’)

๋”ฐ๋ผ์„œ, num == *pointer1 == **pointer2 == 10 ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.