728x90

๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ

- ๋ณ€์ˆ˜๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ๋œ๋‹ค.
- C์–ธ์–ด์—์„œ ๋ชจ๋“  ๋ณ€์ˆ˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์ƒ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
- ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„์œผ๋กœ, ๊ทธ ์œ„์น˜๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • ๋ณ€์ˆ˜์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณ€์ˆ˜ ์•ž์— &(์ฃผ์†Œ ์—ฐ์‚ฐ์ž)๋ฅผ ๋ถ™์ธ๋‹ค.
  • ์ฃผ์†Œ๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ '์‹œ์ž‘ ์ฃผ์†Œ'๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
#include <stdio.h>

int main(void)
{
	int num = 10;
    printf("%p\n", &num);
    
    return 0;
}

์‹คํ–‰ ๊ฒฐ๊ณผ: 00AFF9F4

โ–ถ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋Š” 00AFF9F4์™€ ๊ฐ™์ด 16์ง„์ˆ˜ ํ˜•ํƒœ์ด๋ฉฐ, printf์—์„œ ์„œ์‹ ์ง€์ •์ž %p๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.

8์ง„์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” %o, 16์ง„์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” %x๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋Š” ๊ณ ์ •๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ปดํ“จํ„ฐ๋งˆ๋‹ค, ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ๋‹ฌ๋ผ์ง„๋‹ค.

 

 

  • intํ˜• ๋ณ€์ˆ˜๋Š” 4๋ฐ”์ดํŠธ์ด๋ฏ€๋กœ intํ˜• ๋ณ€์ˆ˜ num์€ 00AFF9F4๋ฒˆ์ง€์—์„œ๋ถ€ํ„ฐ 00AFF9F7๋ฒˆ์ง€๊นŒ์ง€ ๊ฑธ์ณ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ณ€์ˆ˜ num์ด ์ €์žฅ๋˜๊ธฐ ์‹œ์ž‘ํ•œ ์ฃผ์†Œ 00AFF9F4๊ฐ€ ๋ณ€์ˆ˜ num์˜ ์ฃผ์†Œ๊ฐ’์ด๋‹ค.
  • ์ฃผ์†Œ๊ฐ’ 00AFF9F4 ๋˜ํ•œ 16์ง„์ˆ˜๋กœ ์ •์ˆ˜์ด๋‹ค. 
  • ๋”ฐ๋ผ์„œ ์ฃผ์†Œ๊ฐ’๋„ ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๊ณ , ์ด ์ฃผ์†Œ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๊ฐ€ ๋ฐ”๋กœ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์ด๋‹ค.

ํฌ์ธํ„ฐ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…

  • ํฌ์ธํ„ฐ(pointer)๋ž€ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฃผ์†Œ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜
  • *(์ฐธ์กฐ์—ฐ์‚ฐ์ž)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธ
  • ์ž๋ฃŒํ˜•์€ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๊ณ ์ž ํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•์„ ๋ช…์‹œํ•œ๋‹ค.
์ž๋ฃŒํ˜• * ํฌ์ธํ„ฐ์ด๋ฆ„;

 

  • ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š์€ ์ฑ„๋กœ ์ฐธ์กฐ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์˜๋„ํ•˜์ง€ ์•Š์€ ๋ฉ”๋ชจ๋ฆฌ ์žฅ์†Œ์— ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ๋œ๋‹ค.
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํฌ์ธํ„ฐ์˜ ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”๋ฅผ ํ•จ๊ป˜ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค,
์ž๋ฃŒํ˜• * ํฌ์ธํ„ฐ์ด๋ฆ„ = &๋ณ€์ˆ˜์ด๋ฆ„;
์ž๋ฃŒํ˜• * ํฌ์ธํ„ฐ์ด๋ฆ„ = ์ฃผ์†Œ๊ฐ’;
#include <stdio.h>
 
int main(void) {
 
    int num;    // ์ผ๋ฐ˜ ๋ณ€์ˆ˜ ์„ ์–ธ
    int * pnum;    // ํฌ์ธํ„ฐ ์„ ์–ธ
    
    pnum = &num;    // ํฌ์ธํ„ฐ์— num ์ฃผ์†Œ ๋Œ€์ž…
    *pnum = 10;    // ํฌ์ธํ„ฐ๋กœ ๋ณ€์ˆ˜ num์— 10 
 
    printf("๋ณ€์ˆ˜๋ช…์œผ๋กœ num ๊ฐ’ ์ถœ๋ ฅ : %d\n", num);
    printf("ํฌ์ธํ„ฐ๋กœ num ๊ฐ’ ์ถœ๋ ฅ : %d\n", *pnum);
 
    return 0;
}

์‹คํ–‰๊ฒฐ๊ณผ
๋ณ€์ˆ˜๋ช…์œผ๋กœ num ๊ฐ’ ์ถœ๋ ฅ : 10
ํฌ์ธํ„ฐ๋กœ num ๊ฐ’ ์ถœ๋ ฅ : 10

ํฌ์ธํ„ฐ์— num์˜ ์‹œ์ž‘ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•˜๋ฉด ํฌ์ธํ„ฐ pnum์€ ๋ณ€์ˆ˜ num์˜ ์œ„์น˜๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค.

โ–ถ ํฌ์ธํ„ฐ๊ฐ€ ์–ด๋–ค ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•œ ๊ฒฝ์šฐ '๊ฐ€๋ฆฌํ‚จ๋‹ค'๋ผ๊ณ  ํ•˜๊ณ , pnum์ด num์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

โ–ถ ํฌ์ธํ„ฐ pnum์ด num์„ ๊ฐ€๋ฅดํ‚ค๋ฏ€๋กœ *pnum์— 10์„ ๋Œ€์ž…ํ•˜๋ฉด ๊ฒฐ๊ตญ num์— 10์„ ๋Œ€์ž…ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.

 

์ด์ค‘ ํฌ์ธํ„ฐ

  • ์ด์ค‘ ํฌ์ธํ„ฐ(ํฌ์ธํ„ฐ์˜ ํฌ์ธํ„ฐ) = ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋˜ ๋‹ค๋ฅธ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜
  • ํฌ์ธํ„ฐ๋ฅผ ์„ ์–ธํ•  ๋•Œ ** ์‚ฌ์šฉํ•˜์—ฌ ์ด์ค‘ ํฌ์ธํ„ฐ๋ฅผ ์„ ์–ธ
  • ํฌ์ธํ„ฐ๋ฅผ ์„ ์–ธํ•  ๋•Œ *์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ์„œ ์‚ผ์ค‘ ํฌ์ธํ„ฐ, ์‚ฌ์ค‘ ํฌ์ธํ„ฐ ๊ทธ ์ด์ƒ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค
์ž๋ฃŒํ˜• ** ํฌ์ธํ„ฐ์ด๋ฆ„;
#include <stdio.h>

int main(void)
{
	int * ptr;	// ํฌ์ธํ„ฐ ์„ ์–ธ
    int ** dptr;	// ์ด์ค‘ ํฌ์ธํ„ฐ ์„ ์–ธ
    int num = 10;
    
    ptr = &num;			// ํฌ์ธํ„ฐ์— num ์ฃผ์†Œ ๋Œ€์ž…
    
    dptr = &ptr;		// ์ด์ค‘ ํฌ์ธํ„ฐ์— ptr ์ฃผ์†Œ ๋Œ€์ž…
    
    printf("%d\n". **dptr);	// ํฌ์ธํ„ฐ๋ฅผ ๋‘ ๋ฒˆ ์—ญ์ฐธ์กฐํ•˜์—ฌ num์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— ์ ‘๊ทผ
    
    return 0;
}

์‹คํ–‰๊ฒฐ๊ณผ
10

โ–ถ dptr์€ ptr์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๊ณ , ptr์€ num์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ์— *dptr์€ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜ ptr์„, **dptr์€ ๋ณ€์ˆ˜ num์„ ์˜๋ฏธํ•˜๊ฒŒ ๋œ๋‹ค.


ํฌ์ธํ„ฐ์™€ const

  • ๋ณ€์ˆ˜์— ์‚ฌ์šฉํ•˜๋Š” const์™€๋Š” ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋ฉฐ ํฌ์ธํ„ฐ์— ์‚ฌ์šฉํ•˜๋Š” const๋Š” ์ƒ์ˆ˜๋กœ ๋งŒ๋“œ๋Š” ์„ ์–ธ์ด ์•„๋‹Œ, ํฌ์ธํ„ฐ๋ฅผ ํ†ตํ•œ ๋ณ€๊ฒฝ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์„ ์–ธ
  • const๋Š” ๋ณ€์ˆ˜๋ฅผ ์ƒ์ˆ˜ํ™”์‹œํ‚ค๋Š” ์—ญํ• , ํฌ์ธํ„ฐ๊ฐ€ ์ƒ์ˆ˜ํ™” ๋˜๋Š” ๊ฒƒ์ด์ง€ ๋ณ€์ˆ˜๊ฐ€ ์ƒ์ˆ˜ํ™” ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.
  • ์ฆ‰, ๋ณ€์ˆ˜ num์€ ์–ธ์ œ๋“ ์ง€ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‚˜. ํฌ์ธํ„ฐ๋ฅผ ํ†ตํ•ด ๋ณ€์ˆ˜ num ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ๋งŒ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.
#include <stdio.h>

int main(void)
{
	int num = 10;
    const int * p = &num;
    
    *p = 20;	โ–ถ ์ปดํŒŒ์ผ ์—๋Ÿฌ
    
    printf("๋ณ€์ˆ˜ num : %d\n", *p);
    
    return 0;
}
#include <stdio.h>

int main(void)
{
	int num = 10;
    const int * p = &num;
    
    num = 20;	โ–ถ ์ปดํŒŒ์ผ ์„ฑ๊ณต
    
    printf("๋ณ€์ˆ˜ num : %d\n",*p);
    
    return 0;
}

์‹คํ–‰๊ฒฐ๊ณผ
20

 

 

์ถœ์ฒ˜: https://sejong-kr.libguides.com/c.php?g=942235&p=6822366

'ํ”„๋กœ๊ทธ๋ž˜๋ฐ > C์–ธ์–ด' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ํฌ์ธํ„ฐ & ํ•จ์ˆ˜  (0) 2024.08.16
ํฌ์ธํ„ฐ & ๋ฐฐ์—ด  (0) 2024.08.16
๋ฐฐ์—ด  (0) 2024.08.14
ํ•จ์ˆ˜  (0) 2024.08.14
๋ฐ˜๋ณต๋ฌธ  (0) 2024.07.31

+ Recent posts