๐ ์ ์ C์ธ์ด ๊ธฐ์ด๊ฐ ๋๋๊ฐ๋ค. ๋ค์ฐจ์ ๋ฐฐ์ด๋ถํฐ ํฌ์ธํฐ์ ํฌ์ธํฐ, void ํฌ์ธํฐ๊น์ง 16์ฅ๋ถํฐ 20์ฅ๊น์ง์ ๋ด์ฉ์ ๋ค๋ฃฌ๋ค.
16. ๋ค์ฐจ์ ๋ฐฐ์ด
โ 2์ฐจ์ ๋ฐฐ์ด / ์ ์ธ / ์ ๊ทผ / sizeof
ex) ์ธ๋ก๊ฐ 3, ๊ฐ๋ก๊ฐ 4์ธ intํ 2์ฐจ์ ๋ฐฐ์ด ์ ์ธ์ int arr1[3][4]; (์ธ๋ก๊ฐ 3, ๊ฐ๋ก๊ฐ 4์ธ 2์ฐจ์ ๋ฐฐ์ด)
ex) ์ฌ๋ฌ ๋ฐฐ์ด์ ํํ
→ sizeof ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ฉด 2์ฐจ์ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๊ณ์ฐํ ์ ์๋ค. ์ธ๋ก ๊ธธ์ด x ๊ฐ๋ก ๊ธธ์ด x ์๋ฃํ ํฌ๊ธฐ๋ก ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ ๋์จ๋ค.
#include <stdio.h>
int main(void) {
int arr1[3][4];
int arr2[7][9];
printf("arr1 size: %d\n",sizeof(arr1)); //arr1 size: 48
printf("arr2 size: %d\n",sizeof(arr2)); //arr2 size: 252
return 0;
}
→ 2์ฐจ์ ๋ฐฐ์ด์์์ ์ ๊ทผ์ arr[x][y]๋ก ์ง์ = ๋์ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด ๊ธฐ์กด ๋ฐฐ์ด ์์์ ๊ฐ์ ๋ฐ๊ฟ ์ ์๋ค.
ex) ๋น๋ผ์ ๊ฐ ๊ฑฐ์ฃผ์ธ์ ์ ๋ ฅ๋ฐ๊ณ ์ธต๋ณ ์ธ์์ ํฉ ์ถ๋ ฅํ๊ธฐ
#include <stdio.h>
int main(void) {
int villa [4][2];
int popu, i, j;
for(i=0;i<4;i++){
for(j=0;j<2;j++){
printf("floor %d ho %d population: ",i+1,j+1);
scanf("%d",&villa[i][j]);
}
}
for(int i = 0; i < 4; i++){
popu = 0;
popu += villa[i][0];
popu += villa[i][1];
printf("%d floor total population: %d\n",i+1,popu);
}
return 0;
}
/*
floor 1 ho 1 population: 2
floor 1 ho 2 population: 4
floor 2 ho 1 population: 3
floor 2 ho 2 population: 5
floor 3 ho 1 population: 2
floor 3 ho 2 population: 6
floor 4 ho 1 population: 4
floor 4 ho 2 population: 3
1 floor total population: 6
2 floor total population: 8
3 floor total population: 8
4 floor total population: 7
*/
โก 2์ฐจ์ ๋ฐฐ์ด์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
→ ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ๋ ๋ฉ๋ชจ๋ฆฌ์ ์ฃผ์ ๊ฐ์ 1์ฐจ์์ ๊ตฌ์กฐ. ๋ฐ๋ผ์ 2์ฐจ์ ๋ฐฐ์ด๋ ๋ฉ๋ชจ๋ฆฌ ์์๋ 1์ฐจ์์ ํํ๋ก ์กด์ฌํ๋ค.
→ Application - ์ด์์ฒด์ (OS) - ๋ฌผ๋ฆฌ์ ๋ฉ๋ชจ๋ฆฌ ์ด๋ ๊ฒ 3๋จ ๊ตฌ์กฐ๋ก ๋์ด ์๋๋ฐ, ์ด์์ฒด์ OS๊ฐ ๋ฌผ๋ฆฌ์ ์ธ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ์ฉํ๋ ๋ฐ ์ญํ ์์
ex) int arr[2][3]์ ๋ฉ๋ชจ๋ฆฌ์ ๊ตฌ์กฐ
: ์ค์ ์ ๊ทธ๋ฆผ์ ํํ๋ก ๋ฐฐ์ด์ด ํ ๋น๋๋ ์ง ํ์ธํ๊ธฐ ์ํด 2์ฐจ์ ๋ฐฐ์ด์์์ ์ฃผ์ ๊ฐ ์ถ๋ ฅ
#include <stdio.h>
int main(void) {
int arr[2][3];
int i, j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf("%p\n",&arr[i][j]);
return 0;
}
/*
0x7ffedddb8b10
0x7ffedddb8b14
0x7ffedddb8b18
0x7ffedddb8b1c
0x7ffedddb8b20
0x7ffedddb8b24
*/
โข 2์ฐจ์ ๋ฐฐ์ด ์ด๊ธฐํ
(1) ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ
→ 1์ฐจ์ ๋ฐฐ์ด๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก 2์ฐจ์ ๋ฐฐ์ด๋ ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ๊ฐ ๊ฐ๋ฅํ๋ค. ์ผ๋ถ ์์์ ๋ํด์๋ ์ด๊ธฐํ ์๋ต ๊ฐ๋ฅ. ๋น๊ฒ ๋๋ ๊ณต๊ฐ์ 1์ฐจ์ ๋ฐฐ์ด์ ๊ฒฝ์ฐ์ ๋ง์ฐฌ๊ฐ์ง๋ก 0์ผ๋ก ์ด๊ธฐํ๋๋ค.
ex) ํ๋จ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ 3๊ฐ์ง
#include <stdio.h>
int main(void) {
int i, j;
int arr1[3][3]={
{1,2,3},
{4,5,6},
{7,8,9}
};
int arr2[3][3]={
{1},
{4,5},
{7,8,9}
};
int arr3[3][3] = {1, 2, 3, 4, 5, 6, 7};
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%d ", arr1[i][j]);
printf("\n");
}
printf("\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%d ", arr2[i][j]);
printf("\n");
}
printf("\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%d ", arr3[i][j]);
printf("\n");
}
printf("\n");
return 0;
}
/*
1 2 3
4 5 6
7 8 9
1 0 0
4 5 0
7 8 9
1 2 3
4 5 6
7 0 0
*/
(2) ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ์๋ ค์ฃผ์ง ์๊ณ ์ด๊ธฐํ
→ ๋ฐฐ์ด์ ์ธ๋ก๊ธธ์ด๋ง ์๋ต์ด ๊ฐ๋ฅํ๋ค ex) int arr1[][4] = {1, 2, 3, 4, 5, 6, 7, 8};
→ ๋ฐฐ์ด์ ๊ฐ๋ก๊ธธ์ด๋ ์๋ต์ด ๋ถ๊ฐ๋ฅ. ๋ฐฐ์ด ๋ด์ ์์ ๊ฐ๋ก๊ธธ์ด๊ฐ ํ์ ๋ ์ํ์์ 1ํ์์๋ถํฐ ์์ํด ์์๊ฐ ๋์ด๋๊ธฐ์ ๊ฐ๋ก๊ธธ์ด๋ ํ์๋ก ์์์ผ ํ๋ค.
ex1) ๊ฐ๋ก ๊ธธ์ด๊ฐ 9, ์ธ๋ก ๊ธธ์ด๊ฐ 3์ธ intํ 2์ฐจ์ ๋ฐฐ์ด์ ์ ์ธํ์ฌ ๊ตฌ๊ตฌ๋จ ์ค 2๋จ, 3๋จ, 4๋จ์ ์ ์ฅํ์. ๊ทธ๋ฆฌ๊ณ 2์ฐจ์ ๋ฐฐ์ด ์ถ๋ ฅ
#include <stdio.h>
int main(void) {
int gugu[3][9];
for(int i = 0; i < 3; i++){
printf("%d dan\n",i+2);
for(int j = 0; j < 9; j++){
printf("result of %d x %d? ",i+2,j+1);
scanf("%d",&gugu[i][j]);
}
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 9; j++){
printf("%d ",gugu[i][j]);
}
printf("\n");
}
return 0;
}
/*
2 dan
result of 2 x 1? 2
result of 2 x 2? 4
result of 2 x 3? 6
result of 2 x 4? 8
result of 2 x 5? 10
result of 2 x 6? 12
result of 2 x 7? 14
result of 2 x 8? 16
result of 2 x 9? 18
3 dan
result of 3 x 1? 3
result of 3 x 2? 6
result of 3 x 3? 9
result of 3 x 4? 12
result of 3 x 5? 15
result of 3 x 6? 18
result of 3 x 7? 21
result of 3 x 8? 24
result of 3 x 9? 27
4 dan
result of 4 x 1? 4
result of 4 x 2? 8
result of 4 x 3? 12
result of 4 x 4? 16
result of 4 x 5? 20
result of 4 x 6? 24
result of 4 x 7? 28
result of 4 x 8? 32
result of 4 x 9? 36
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
*/
ex2) ๋ฐฐ์ด A = {1, 2, 3, 4, 5, 6, 7, 8}๊ณผ ๋ฐฐ์ด B = {1, 5, 2, 6, 3, 7, 4, 8} ๊ฐ๊ฐ [2][4] ํํ, [4][2] ํํ์ ๋ฐฐ์ด์์ ๋ฐฐ์ด A๋ ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ ์งํ. ๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด B์ญ์ ์ด๊ธฐํ๋ฅผ ์งํํ์ง๋ง, ๋ฐ๋์ ๋ฐฐ์ด A์ ์ ์ฅ๋ ๊ฐ์ ์ด์ฉํด์ ์ด๊ธฐํ ์งํํ์
→ [a][b] index๊ฐ ๋ฐฐ์ด B๋ก ๋์ด๊ฐ๋ฉด์ [b][a] index๋ก ๋ฐ๋๋ค.
#include <stdio.h>
int main(void) {
int A[2][4] = {1,2,3,4,5,6,7,8};
int B[4][2];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 2; j++){
B[i][j] = A[j][i];
}
for(int i = 0; i< 4; i++){
for(int j = 0; j < 2; j++){
printf("%d ", B[i][j]);
}
printf("\n");
}
return 0;
}
/*
1 5
2 6
3 7
4 8
*/
ex3) ์ฑ์ ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ. ๊ตญ์ด / ์์ด / ์ํ / ๊ตญ์ฌ ์ด๋ ๊ฒ ๋ค ๊ณผ๋ชฉ์ด๊ณ ํ์์ ๋ค ์ฌ๋์ด๋ค. ํ๋ก๊ทธ๋จ ์ฌ์ฉ์๋ก๋ถํฐ ๋ค ์ฌ๋์ ๋ค ๊ณผ๋ชฉ ์ ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ๋ฏธ๋ฆฌ ์ ์ธํด ๋์ ๋ฐฐ์ด์ ์ด์ ๊น์ง ์ด 5 x 5 2์ฐจ์ ๋ฐฐ์ด๋ก ์ ์ฅ. ๋ง์ง๋ง์ ๋ฐฐ์ด ์ถ๋ ฅ
#include <stdio.h>
int main(void) {
int arr[5][5] ={0};
int sum_of_student = 0;
int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
for(int i = 0; i < 4; i++){
sum_of_student = 0;
printf("student %d grade\n",i+1);
for(int j = 0; j < 4; j++){
printf("grades: ");
scanf("%d",&arr[i][j]);
sum_of_student += arr[i][j];
}
arr[4][0] += arr[i][0];
arr[4][1] += arr[i][1];
arr[4][2] += arr[i][2];
arr[4][3] += arr[i][3];
arr[i][4] = sum_of_student;
arr[4][4] += sum_of_student;
}
//print array
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
printf("%-3d", arr[i][j]);
}
printf("\n");
}
return 0;
}
/*
student 1 grade
grades: 5
grades: 4
grades: 6
grades: 5
student 2 grade
grades: 3
grades: 6
grades: 9
grades: 3
student 3 grade
grades: 4
grades: 8
grades: 2
grades: 7
student 4 grade
grades: 3
grades: 8
grades: 9
grades: 7
5 4 6 5 20
3 6 9 3 21
4 8 2 7 21
3 8 9 7 27
15 26 26 22 89
*/
โฃ 3์ฐจ์ ๋ฐฐ์ด : ๊ฐ๋ก์ ์ธ๋ก 2์ฐจ์ ๋ฐฐ์ด์ ๋์ด์ ๊ฐ๋
์ด ์ถ๊ฐ๋์ด 3์ฐจ์ ๋ฐฐ์ด ํ์ฑ
ex1) int arr1[2][3][4]; (๋์ด๊ฐ 2, ์ธ๋ก๊ฐ 3, ๊ฐ๋ก๊ฐ 4์ธ intํ 3์ฐจ์ ๋ฐฐ์ด)
ex2) double arr2[5][5][5]; (๋์ด, ์ธ๋ก, ๊ฐ๋ก ๋ชจ๋ 5์ธ doubleํ 3์ฐจ์ ๋ฐฐ์ด)
→ 3์ฐจ์ ๋ฐฐ์ด์ sizeof() ์ฐ์ฐ ๊ฒฐ๊ณผ ๋์ด x ๊ฐ๋ก๊ธธ์ด x ์ธ๋ก๊ธธ์ด x sizeof(๋ฐ์ดํฐ ํ์
)์ด ๋์จ๋ค.
ex) ๊ฐ class๋ณ ํ์๋ณ ์ฑ์ (ํ class์ ํ์๋ณ ์ฑ์ ์ด ์๋ 2์ฐจ์ ๋ฐฐ์ด์ด ์ฌ๋ฌ ๊ฐ์ class๋ก ๋ชจ์ธ 3์ฐจ์ ๋ฐฐ์ด)
#include <stdio.h>
int main(void) {
int mean = 0, i, j;
int record[3][3][2] = {
{
{70,80}, //A class student 1 grades
{94,90}, //A class student 2 grades
{70,85} //A class student 3 grades
},
{
{83,90}, //B class student 1 grades
{95,60}, //B class student 2 grades
{90,82} //B class student 3 grades
},
{
{98,89}, //C class student 1 grades
{99,94}, //C class student 2 grades
{91,87} //C class student 3 grades
}
};
for(i=0;i<3;i++)
for(j=0;j<2;j++)
mean += record[0][i][j];
printf("mean of A class grades: %g\n",(double)mean/6);
mean = 0;
for(i=0;i<3;i++)
for(j=0;j<2;j++)
mean += record[1][i][j];
printf("mean of B class grades: %g\n",(double)mean/6);
mean=0;
for(i=0;i<3;i++)
for(j=0;j<2;j++)
mean += record[2][i][j];
printf("mean of C class grades: %g\n",(double)mean/6);
return 0;
}
/*
mean of A class grades: 81.5
mean of B class grades: 83.3333
mean of C class grades: 93
*/
→ ์ฆ 3์ฐจ์ ๋ฐฐ์ด์ ์ฌ๋ฌ ๊ฐ์ 2์ฐจ์ ๋ฐฐ์ด์ด ๋ชจ์ฌ ์๋ ํํ๋ก ์ดํดํ๋ ๊ฒ์ด ๋ ํฉ๋ฆฌ์ ์ด๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก 2์ฐจ์ ๋ฐฐ์ด์ 1์ฐจ์ ๋ฐฐ์ด์ด ์ฌ๋ฟ ๋ชจ์ฌ ์๋ ํํ๋ก ์ฝ๊ฒ ์ดํดํ ์ ์๋ค.
→ ์ ์์์ ๊ฒฝ์ฐ, ๊ฐ์ฅ ์๋ถ๋ถ์ ์์นํ 2์ฐจ์ ๋ฐฐ์ด์ด ์ฒซ๋ฒ์งธ ํ๊ธ์ด ๋๊ณ , ๊ทธ ๋ค์์ 2์ฐจ์ ๋ฐฐ์ด์ด ๋๋ฒ์งธ ํ๊ธ ์์ผ๋ก ๋๋ ๊ฒ์ด๋ค.
17. ํฌ์ธํฐ์ ํฌ์ธํฐ
โค '์ด์ค ํฌ์ธํฐ(๋๋ธ ํฌ์ธํฐ)': ํฌ์ธํฐ์ ํฌ์ธํฐ๋ก, ํฌ์ธํฐ ๋ณ์๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ ๋ค๋ฅธ ํฌ์ธํฐ ๋ณ์๋ฅผ ๋ปํ๋ค. ํฌ์ธํฐ ๋ณ์์ ์ ์ธ์ ์ฌ์ฉ๋๋ * ์ฐ์ฐ์๋ฅผ ๋ ๊ฐ ์ด์ด์ ์ ์ธํ๋ค. ์๋ฅผ ๋ค์ด intํ ์ด์ค ํฌ์ธํฐ๋ int ** dptr๋ก ํํ ๊ฐ๋ฅํ๋ค.
ex) double *ptr = # double **dptr = &ptr;
→ ์ ์ฝ๋์์ doubleํ ์ด์คํฌ์ธํฐ dptr์ ํฌ์ธํฐ ๋ณ์ ptr์ ๊ฐ๋ฆฌํค๊ณ , ํฌ์ธํฐ ๋ณ์ ptr์ ๋ณ์ num์ ๊ฐ๋ฆฌํจ๋ค. ๋ฐ๋ผ์ *dptr์ ํฌ์ธํฐ ๋ณ์ ptr์ ์๋ฏธํ๊ณ , *(*dptr)์(**dptr๋ก๋ ํํ ๊ฐ๋ฅ) ๋ณ์ num์ ๋ปํ๋ค.
ex) ์์ : ๋ณ์ num์ ์ฃผ์๊ฐ ์ ์ฅ๋ ptr, ptr์ ์ฃผ์๊ฐ ์ ์ฅ๋ dptr / ๊ทธ๋ฆฌ๊ณ ptr2 ํฌ์ธํฐ ๋ณ์๋ฅผ ์ ์ธํ๊ณ dptr์ ์ฌ์ฉํด ptr2์ ptr์ ์ฃผ์๋ฅผ ์ ์ฅ, ptr2๋ num์ ๊ฐ๋ฆฌํค๊ฒ๋ dptr์ ์ฌ์ฉ
#include <stdio.h>
int main(void) {
double num = 3.14;
double *ptr = #
double **dptr = &ptr;
double *ptr2;
printf("%9p %9p\n",ptr, *dptr);
printf("%9g %9g\n", num, **dptr);
ptr2 = *dptr;
*ptr2 = 10.99;
printf("%9g %9g\n", num, **dptr);
return 0;
}
/*
0x7ffeb371de80 0x7ffeb371de80
3.14 3.14
10.99 10.99
*/
→ ์๋ ๊ทธ๋ฆผ์ ์ฐธ๊ณ ํ๋ฉด
: dptr์ด๋ผ๋ ๋๋ธ ํฌ์ธํฐ๋ฅผ ํ์ฉํด์ ptr2 ํฌ์ธํฐ ๋ณ์๊ฐ num์ ๊ฐ๋ฆฌํค๊ฒ๋ ํ๋ค. ์ฆ, ptr2 ๋ณ์์ dptr์ด ๊ฐ๋ฆฌํค๋, ์ฆ num์ ๋ณ์๋ฅผ ์ ์ฅํด์ค์ผ๋ก์จ ์์ฐ์ค๋ฝ๊ฒ ptr2๋ผ๋ ์ ํฌ์ธํฐ ๋ณ์๊ฐ num์ ๊ฐ๋ฆฌํค๊ฒ ํ๋ค.
โฅ ํฌ์ธํฐ ๋ณ์ ๋์์ call-by-reference
โ ์๋ชป๋ ์์ - num1๊ณผ num2์ ๊ฐ์ด ์๋ก ์๋ฐ๋๋ค. โ
#include <stdio.h>
void SwapIntPtr(int *p1, int *p2){
int * temp = p1;
p1 = p2;
p2 = temp;
}
int main(void) {
int num1 = 10, num2 = 20;
int *ptr1 = &num1;
int *ptr2 = &num2;
printf("*ptr1, *ptr2: %d %d\n", *ptr1, *ptr2);
SwapIntPtr(ptr1, ptr2);
printf("*ptr1, *ptr2: %d %d\n", *ptr1, *ptr2);
return 0;
}
/*
*ptr1, *ptr2: 10 20
*ptr1, *ptr2: 10 20
*/
: int *p1 = ptr1์ด๋ฏ๋ก ptr1์ num1์ ์ฃผ์์ด๋ฏ๋ก, int *p1 = &num1, int *p2 = &num2๋ก p1๊ณผ p2๋ ํฌ์ธํฐ ๋ณ์์ด๋ฉฐ ๊ฐ๊ฐ num1๊ณผ num2๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ค.(์ฆ ๊ฐ๊ฐ num1๊ณผ num2์ ์ฃผ์๊ฐ์ ๊ฐ๊ฒ ๋๋ค). ์ด ๋, p1๊ณผ p2์ ๊ฐ์ swapํ์ผ๋ฏ๋ก p1์๋ num2์ ์ฃผ์, p2์๋ num1์ ์ฃผ์๊ฐ ๋ค์ด๊ฐ, ๊ฒฐ๊ตญ์ p1์ num2๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๊ณ , p2๋ num1์ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค. ๋ฐ๋ผ์ ptr1๊ณผ ptr2๋ ์ฌ์ ํ ๊ฐ๊ฐ num1๊ณผ num2๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค. ptr1์ ์ ์ฅ๋ ์ฃผ์์ธ num1์ ์ฃผ์๋ฅผ p1 ๋ณ์์ ๋์ , ptr2๋ ๋ง์ฐฌ๊ฐ์ง์ด๋ค(ํจ์๋ฅผ ํธ์ถํ ๋) <์๋ ๊ทธ๋ฆผ ์ฐธ์กฐ>
→ ์ฒซ๋ฒ์งธ ๊ณผ์ ์์ ํจ์ ๋งค๊ฐ๋ณ์๊ฐ ๋ง๋ค์ด์ง ๋ p1๊ณผ p2์ ๊ฐ๊ฐ ptr1๊ณผ ptr2์ ๋ณ์ ๊ฐ์ด ๋์ ๋๊ณ , ๋๋ฒ์งธ ํจ์ ๋ด๋ถ ๊ณผ์ ์์ swap์ด ์ผ์ด๋๋ฉด์ p1๊ฐ๊ณผ p2๊ฐ์ด ์๋ก ๋ฐ๋๋ค. ์ด๋ ๊ณง, ์ธ๋ฒ์งธ ๊ณผ์ ์ธ ์ฐธ์กฐ ๋์์ด ์๋ก ๋ฐ๋๋ค๋ ๊ฒ์ ๋ปํ๋ค.
โป ์ค์ํ ๊ฑด, ์ฌ์ ํ ptr1๊ณผ ptr2๋ num1๊ณผ num2๋ฅผ ๊ฐ๋ฆฌํจ๋ค๋ ์ . ๋ณํ ์๋ค. โป
โ ์ฌ๋ฐ๋ฅธ ์์ - num1๊ณผ num2์ ๊ฐ์ด ์๋ก ๋ฐ๋๋ค. โ
: ์ฌ๋ฐ๋ฅด๊ฒ num1๊ณผ num2 ๋ณ์๊ฐ์ด ์๋ก ๋ฐ๋๊ฒ ํ๊ธฐ ์ํด์๋ ptr1๊ณผ ptr2์ ๊ฐ์ด ์๋ก ๋ฐ๋์ด์ผ ํ๋ค. ์ฆ, p1๊ณผ p2๊ฐ num1๊ณผ num2๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํด์๋ ์๋๊ณ p1๊ณผ p2๊ฐ ptr1๊ณผ ptr2๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํ๊ธฐ ์ํด ๋๋ธํฌ์ธํฐ๋ก ๋ง๋ค์ด์ผ ํ๋ค.
#include <stdio.h>
void SwapIntPtr(int **dp1, int **dp2){
int * temp = *dp1;
*dp1 = *dp2;
*dp2 = temp;
}
int main(void) {
int num1 = 10, num2 = 20;
int *ptr1 = &num1;
int *ptr2 = &num2;
printf("*ptr1, *ptr2: %d %d\n", *ptr1, *ptr2);
SwapIntPtr(&ptr1, &ptr2);
printf("*ptr1, *ptr2: %d %d\n", *ptr1, *ptr2);
return 0;
}
/*
*ptr1, *ptr2: 10 20
*ptr1, *ptr2: 20 10
*/
: ๋๋ธํฌ์ธํฐ dp1๊ณผ dp2๊ฐ ๊ฐ๊ฐ ptr1๊ณผ ptr2๋ฅผ ๊ฐ๋ฆฌํจ๋ค. dp1์ ๊ฐ๋ฆฌํค๋ ptr1์ ์ ์ฅ๋ ๊ฐ๊ณผ dp2๋ฅผ ๊ฐ๋ฆฌํค๋ ptr2์ ์ ์ฅ๋ ๊ฐ์ด ์๋ก ๋ฐ๋๋ swap ๊ณผ์ ์ด ๋ฐ์ํ๋ฏ๋ก, ptr1์๋ num2์ ์ฃผ์๊ฐ์ด ๋ค์ด๊ฐ๊ณ , ptr2์๋ num1์ ์ฃผ์๊ฐ์ด ๋ค์ด๊ฐ๋ค. ๋ฐ๋ผ์ *ptr1๊ณผ *ptr2์ ๊ฐ์ด ์๋ก ๋ฐ๋์ ํ์ธํ ์ ์๋ค.
18. ๋ค์ฐจ์ ๋ฐฐ์ด๊ณผ ํฌ์ธํฐ์ ๊ด๊ณ
โฆโงโจโฉโชโซโฌโญโฎ
โฏโฐโฑโฒโณ
* ์ถ์ฒ <์ค์ฑ์ฐ ์ดํ C ํ๋ก๊ทธ๋๋ฐ>
'C, C++ > Fundamentals' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
C Basics 4/ (์ดํ ํ๋ก๊ทธ๋๋ฐ) (0) | 2023.07.05 |
---|---|
C Basics 3/ (์ดํ Cํ๋ก๊ทธ๋๋ฐ) (0) | 2023.06.27 |
C Basics 2/ (์ดํ Cํ๋ก๊ทธ๋๋ฐ) (0) | 2023.06.26 |
C Basics 1/ (0) | 2023.06.25 |
C fundamentals (1) (1) | 2023.06.16 |
๋๊ธ