본문 바로가기

C언어

call by value 와 reference 의 차이

#include <stdio.h>

void pswap(int *p1, int *p2);

int main()
{
 int A=10,B=20;
 int *pA,*pB;
 pA=&A,pB=&B;
 pswap(pA,pB);
// 함수 호출 후
printf("pA가 가리키는 변수: %d\n",*pA);
printf("pB가 가리키는 변수: %d\n",*pB);
return 0;
}

void pswap(int *p1, int *p2)
{
int *TEMP;
TEMP=*p1;
*p1=*p2;
*p2=TEMP;
}

//-------------------------------------------------------------------------------------
#include <stdio.h>

void pswap(int **p1, int **p2);

int main()
{
 int A=10,B=20;
 int *pA,*pB;
 pA=&A,pB=&B;
 pswap(&pA,&pB);
// 함수 호출 후
printf("pA가 가리키는 변수: %d\n",*pA);
printf("pB가 가리키는 변수: %d\n",*pB);
return 0;
}

void pswap(int **p1, int **p2)
{
int *TEMP;
TEMP=*p1;
*p1=*p2;
*p2=TEMP;
}

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

scanf 두개의 정수 연산  (0) 2010.04.23
환경변수 경로  (0) 2010.03.08
volatile  (0) 2010.01.21
트리구조  (0) 2010.01.19
ch(x<0)? '-':'+'; // 참이면 -(전자) : 거짓이면 +(후자) 실행  (0) 2009.12.26