본문 바로가기

C & C++/C언어

[C] 매개변수의 전달(call by value/call by address 포함)

반응형

 

매개변수의 전달은 크게 call by value 와 call by address로 구분

두 방법은 비슷하지만 각각 다른경우를 포함

 

1. 기본적인 call by value 와 call by address

 

Call by value

Call by address

#include<stdio.h>

void func(int num);

 

void main(){
    int num =10;
    func(num);
}

 

void func(int num){
    printf("num: %d\n", num);
}

#include<stdio.h>

 

void func(int num);

 

void main(){

    int num =10;

    func(&num);

}

 

void func(int *num){

    printf("num: %d\n", *num);

}

위 두방법의 결과는 같지만 값을 전달하느냐와 값이 들어있는 주소를 전달하느냐의 차이가 있다.

 

2. 값에 차이가 발생하는 call by value와 call by address

 

Call by value

Call by address

#include<stdio.h>

void swap(int a, int b);

 

void main()

{

    int x =20,y =50;

    prinft("[1]: %d, %d\n", x, y);

    swap(x, y);

    printf("[4]: %d, %d\n", x, y);

}

 

void swap(int a, int b)

{

    int temp =0;

    printf("[2]: %d, %d\n", a, b);

    temp = a;

    a=b;

    b=temp;

    printf("[3]: %d, %d\n", a, b);

}

 

결과:

[1]: 20,50

[2]: 20,50

[3]: 50,20

[4]: 20,50

 

#include<stdio.h>

void swap(int *a, int *b);

 

void main()

{

    int x =20,y =50;

    prinft("[1]: %d, %d\n", x, y);

    swap(&x, &y);

    printf("[4]: %d, %d\n", x, y);

}

 

void swap(int *a, int *b)

{

    int temp =0;

    printf("[2]: %d, %d\n", *a, *b);

    temp = *a;

    *a=*b;

    *b=temp;

    printf("[3]: %d, %d\n", *a, *b);

}

 

결과:

[1]: 20,50

[2]: 20,50

[3]: 50,20

[4]: 50,20

 

위 예제는 swap함수를 통해서 변수값 또는 변수의주소를 매개변수로 넘겨 a,b 값을 서로 교체하는 예제이다.

 

결과의

[1]은 main에서의 a,b의 값

[2]는 main에서 swap함수로 넘겨준 값

[3]은 swap함수에서 서로 교체된 값

[4]는 swap함수를 호출 후 main함수의 a,b의 값

 

두 방법을 비교해 보면 [4]의 값이 다른것을 알 수 있다.

 

call by value는 main함수의 매개변수를 옮겨 변경은 하였지만

다시 리턴되는 값이 없기때문에 swap함수 내에서만 값이 교체된다.

main함수의 [4]값은 [1]과 동일하게 출력된다.

 

하지만 call by address는 main함수의 매개변수를 옮겨 변경하고

주소를 가리켜 주소 안의 값을 변경하였기 때문에 다시 리턴되는 값이 없어도 swap함수에서의 변수가 변경되고

main함수의 [4]의 값 또한 교체되어 출력된다.

 

3-1. 일차원 배열의 값 전달(정수형)

 

정수 포인터 전달

정수 배열 전달

#include<stdio.h>

 

void func(int *a, int n);

 

void main()

{

    int arr[5] = {1,2,3,4,5};

    func(arr, 5);

}

 

void func(int *a, int n)

{

    int i ;

    for(i=0; i<n, i++){

        printf("%3d", a[i]);

    }

    putchar('\n');

}

 

결과: 1 2 3 4 5

#include<stdio.h>

 

void func(int a[], int n);

 

void main()

{

    int arr[5] = {1,2,3,4,5};

    func(arr, 5);

}

 

void func(int a[], int n)

{

    int i ;

    for(i=0; i<n, i++){

        printf("%3d", a[i]);

    }

    putchar('\n');

}

 

결과: 1 2 3 4 5

오른쪽정렬해서 출력되는 하나의 정수를 3문자의 크기로 출력하는 예제

방법은 다르지만 같은 방식의 일차원 정수형 배열 전달

 

3-2. 일차원 배열의 값 전달(문자열)

 

문자열 포인터 전달

문자열 배열 전달

#include<stdio.h>

 

void func(char *str);

 

void main(){

    char str[] = "hello world";

    func(str);

}

 

void func(char *str){

    printf("%s", str);

}

 

결과: hello world

#include<stdio.h>

 

void func(char *str);

 

void main(){

    char str[] = "hello world";

    func(str);

}

 

void func(char str[]){

    printf("%s", str);

}

 

결과: hello world

방법은 다르지만 같은 방식의 일차원 문자열 배열 전달

 

4. 다차원 배열의 값 전달

 

이차원배열 포인터 전달

이차원배열 배열 전달

#include<stdio.h>

 

void func(int (*a)[5]);

 

void main(){

    int arr[3][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};

    func(arr);

}

 

void func(int (*a)[5]){

    int i ,j;

    for(i=0; i<3; i++){

        for(j=0; j<5;j++){

            printf("%3d", *(*(a+i)+j)); //i만 사용시 *(a+i)

        }

        putchar('\n');

    }

}

 

결과:

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

#include<stdio.h>

 

void func(int (*a)[5]);

 

void main(){

    int arr[3][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};

    func(arr);

}

 

void func(int a[][5]){

    int i ,j;

    for(i=0; i<3; i++){

        for=0; j<5;j++){

            printf("%3d", a[i][j]); //i만 사용시 a[i]

        }

        putchar('\n');

    }

}

 

결과:

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

방법은 다르지만 같은 방식의 이차원 배열 전달

Call by address

Call by address

반응형

'C & C++ > C언어' 카테고리의 다른 글

[C] 전역변수와 static 차이  (0) 2019.10.30
[C] union과 구조체의 차이  (0) 2019.10.30
[C] 코드의 연산자 사용법  (0) 2019.10.30
[C] Type의 갯수 / Char형  (0) 2019.10.30
[C] malloc /calloc  (0) 2019.10.28