pointer - KimTaebin-ai/study_posts GitHub Wiki

void ํฌ์ธํ„ฐ

c ์–ธ์–ด์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž๋ฃŒํ˜•์ด ๋‹ค๋ฅธ ํฌ์ธํ„ฐ๋ผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•˜๋ฉด ์ปดํŒŒ์ผ ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

int main(void)
{
 int a = 10;
 char b = 't';
 int *int_point = &a;
 char *char_point = &b;

 int_point = char_point; // ์˜ค๋ฅ˜๋ฐœ์ƒ
 
 return 0;
}

warning: assignment from incompatible pointer type

ํ•˜์ง€๋งŒ void ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์–ด๋–ค ์ž๋ฃŒํ˜•์˜ ํฌ์ธํ„ฐ๋“  ๋ชจ๋‘ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

int main(void)
{
 int a = 10;
 char b = 't';
 int *int_point = &a;
 char *char_point = &b;

 void *ptr;

 ptr = int_point; // ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค
 ptr = char_point;

 int_point = ptr;
 char_point = ptr;

 return 0;
}

ํ•˜์ง€๋งŒ ์—ญ ์ฐธ์กฐ๋Š” ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋‹ค

๋”ฐ๋ผ์„œ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ํ•จ์ˆ˜์˜ ์˜ค๋ฒ„๋กœ๋”ฉ ํ˜•์‹์ฒ˜๋Ÿผ ๊ฐ€๋ณ€์ ์ธ ํฌ์ธํ„ฐ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์•ผ ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์— ์ธ์ž๊ฐ’์œผ๋กœ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

voidํฌ์ธํ„ฐ๋Š” ์—ญ ์ฐธ์กฐ(์‚ฌ์šฉ)ํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ• ๊นŒ

ํฌ์ธํ„ฐ์˜ ์ฃผ์†Œ๊ฐ’ ์ž์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— 1๋ฐ”์ดํŠธ์”ฉ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” char ํฌ์ธํ„ฐํ˜•์œผ๋กœ ๊ฐ•์ œํ˜•๋ณ€ํ™˜์„ ํ†ตํ•ด ์‚ฌ์šฉํ•œ๋‹ค..!

char ํฌ์ธํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•œ๋‹ค.

๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ จ ๋ฌธ์ œ์—์„œ unsigned char *๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

c์—์„œ๋Š” ๋ถ€ํ˜ธ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ unsigned ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์Œ์ˆ˜๊ฐ€ ๋ถˆํ•„์š”ํ•œ ๊ฒฝ์šฐ unsigned ๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

๋ฉ”๋ชจ๋ฆฌ์˜ ๋ฐ”์ดํŠธ๋ฅผ ์Œ์ˆ˜๋กœ ํ•ด์„ํ•˜๋Š” ๊ฒƒ์€ ์˜๋ฏธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ€ํ˜ธ์™€ ์ƒ๊ด€์—†์ด ์ทจ๊ธ‰ํ•˜๋ ค๋ฉด unsigned char๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•˜๋‹ค.

์ฆ‰, 1๋ฐ”์ดํŠธ(8๋น„ํŠธ)๋ฅผ ํˆฌ๋ช…ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ 

๋”ฐ๋ผ์„œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„  unsigned char ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ธฐ๋ณธ์ด๋ฉฐ ๊ด€๋ก€๋ผ๊ณ  ํ•œ๋‹ค

๋”ฐ๋ผ์„œ voidํฌ์ธํ„ฐ ํ™œ์šฉ ์‹œ unsigend char ํฌ์ธํ„ฐ๋กœ ํ˜• ๋ณ€ํ™˜ํ•˜์—ฌ ์•ˆ์ „ํ•˜๊ฒŒ ๋ฉ”๋ชจ๋ฆฌ์— ์ ‘๊ทผํ•˜์ž

const char * ๋ณ€์ˆ˜๋ช… ๊ณผ char * const ๋ณ€์ˆ˜๋ช… ์˜ ์ฐจ์ด์ 

๋จผ์ € constํ‚ค์›Œ๋“œ๋Š” ์ƒ์ˆ˜ ํ‚ค์›Œ๋“œ๋กœ ๋ณ€ํ•˜์ง€์•Š๊ณ  ์›๋ณธ์„ ์œ ์ง€ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•จ์ˆ˜ ๋‚ด์—์„œ src. ์ฆ‰, ์›๋ณธ์„ ํฌ์ธํ„ฐ๋กœ ๋„˜๊ฒจ์ฃผ์–ด ๊ฐ’์„ ์ฐธ์กฐํ•  ๋•Œ ์›๋ณธ์˜ ๊ฐ’์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜์—ฌ ๋ณด์•ˆ์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

const char *p

์ด๋Š” ํฌ์ธํ„ฐ p๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” ์ž๋ฃŒํ˜•์ด const char ๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. ์ด ๋•Œ ํฌ์ธํ„ฐ p ๋Š” readonly ์˜ ์„ฑ๊ฒฉ์„ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ํฌ์ธํ„ฐ์˜ ์—ญ์ฐธ์กฐ๋ฅผ ํ†ตํ•œ ๊ฐ’ ๋ณ€๊ฒฝ์€ ๋ถˆ๊ฐ€ํ•˜๋‹ค.

int main(void)
{
 char str[20] = "Hello";
 const char *char_point = str;

 *(char_point+1) = 'y'; // ์—๋Ÿฌ
  char_point++; // ๊ฐ€๋Šฅ readonly
  
 return 0;
}

// ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ char_point + 1๋˜ํ•œ const char์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ์ด๋‹ค.

๊ฐ•์ œ ํ˜•๋ณ€ํ™˜์„ ํ†ตํ•ด ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํ•จ์ˆ˜ ์„ค๊ณ„ ๋ฐฉ๋ฉด์œผ๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ strcpy๊ฐ™์€ ํ•จ์ˆ˜์— src(์›๋ณธ)์— const์„ ๊ฑธ์–ด๋‘๊ฒŒ ๋œ๋‹ค๋ฉด ์ด๋Š” readonly๋กœ๋งŒ ์‚ฌ์šฉํ•˜๋ผ๋Š” ๋œป์ด๊ธฐ ๋•Œ๋ฌธ์— ๋˜๋„๋ก ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ ์›๋ณธ์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š๋„๋ก ํ•˜์ž

๊ฐ•์ œ ํ˜•๋ณ€ํ™˜ ๋ฐฉ๋ฒ•

int main(void)
{
 char str[20] = "Hello";
 const char *char_point = str;
s
 *((char *)(char_point+1)) = 'o'; // ๋ณ€๊ฒฝ๊ฐ€๋Šฅ

 printf("%s", char_point);

 return 0;
}

char * const p

char ํ˜•์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ๊ฐ’์ด ์ƒ์ˆ˜๋ผ๋Š” ํฌ์ธํ„ฐ์ด๋‹ค. ์ด๋ฅผ ์ƒ์ˆ˜ ํฌ์ธํ„ฐ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

int main(void)
{
 char str[20] = "Hello";
 char *const char_point = str;

 *(char_point+1) = 'o'; // ๊ฐ€๋Šฅ
 //char_point++; ๋ถˆ๊ฐ€๋Šฅ 

 printf("%s", char_point);

 return 0;
}

ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์ง€๋งŒ, ๊ทธ ์ฃผ์†Œ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋Š” ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ํฌ์ธํ„ฐ ์ž์ฒด๊ฐ€ ์ƒ์ˆ˜์ด๋ฏ€๋กœ ํฌ์ธํ„ฐ์˜ ์ฃผ์†Œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ์—ฐ์‚ฐ์€ ๋ถˆ๊ฐ€ํ•˜๋‹ค.

ํ•จ์ˆ˜ ํฌ์ธํ„ฐ

ํ•จ์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ๋‹ด๋Š” ํฌ์ธํ„ฐ ๋ณ€์ˆ˜์ด๋‹ค. (4 byte)

ํ•จ์ˆ˜๋„ ์ฃผ์†Œ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

void (*fp)();

  • void: ๋ฐ˜ํ™˜๊ฐ’ ์ž๋ฃŒํ˜•
  • (*fp): ํ•จ์ˆ˜ ํฌ์ธํ„ฐ ์‹๋ณ„์ž
  • (): ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†์Œ

์ฆ‰, ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๊ณ  ๋งค๊ฐœ๋ณ€์ˆ˜๋„ ์—†๋Š” fp๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ํ•จ์ˆ˜ํฌ์ธํ„ฐ๋ฅผ ์„ ์–ธํ•œ ๊ฒƒ์ด๋‹ค.

#include <stdio.h>

void print_void()
{
	printf("Hello world\n");
}

int main()
{
	void(*fp)() = print_void;
	fp();
	printf("%p\n", fp);
	printf("%p\n", print_void);

	return 0;
}

์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ์—์„œ %p๋กœ ์ฐ์€ ์ฃผ์†Œ๊ฐ’์€ ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

++ ๋ฐ˜ํ™˜๊ฐ’์ด๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜ ํฌ์ธํ„ฐ

int (*fp)(int, int);

fp๋Š” function pointer์˜ ์•ฝ์–ด

๋Œ•๊ธ€๋ง ํฌ์ธํ„ฐ

๋Œ•๊ธ€๋ง ํฌ์ธํ„ฐ(Dangling Pointer)๋ž€ ์ด๋ฏธ ํ•ด์ œ๋œ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ๋ฅผ ๊ณ„์†ํ•ด์„œ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋งํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์œ ํšจํ•˜์ง€ ์•Š์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Segmentation fault ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

Dangling Pointers ๋ฌธ์ œ์˜ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ๊ฐ€์žฅ ์ข‹์€ ๊ฒƒ์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์†์œผ๋กœ ๋™์  ํ• ๋‹น ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹น ํ•ด์ œํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ๋ช…์‹œ์ ์œผ๋กœ ๋™์ ํ• ๋‹น์„ ํ•ด์ œํ•  ๋•Œ ํ• ๋‹น ํ•ด์ œํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” dangling pointers๊ฐ€ ์—†๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ, run time system์€ ํž™ ๋™์  ๋ณ€์ˆ˜๊ฐ€ ๋” ์ด์ƒ ์œ ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ ์•”์‹œ์ ์œผ๋กœ ํ• ๋‹น์„ ํ•ด์ œํ•ด์•ผ ํ•œ๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ