1. call by value & call by reference
call by value - 값을 전달.
call by reference - 주소를 전달.
#include <stdio.h>
int valueswap (int , int );
int referenceswap (int *, int *);
void main()
{
int a = 10, b = 20;
valueswap (a, b);
printf ("valueswap result : a = %d, b = %d\n", a, b);
printf ("a 와 b 값이 변하지 않음\n");
referenceswap(&a, &b);
printf ("referenceswap result : a = %d, b = %d\n", a, b);
printf ("a 와 b 값이 변함\n");
}
int valueswap (int ap, int bp)
{
int temp;
temp = ap;
ap = bp;
bp = temp;
return 0;
}
int referenceswap (int *ap, int *bp)
{
int temp;
temp = *ap;
*ap = *bp;
*bp = temp;
return 0;
}
2. 빈칸 채우기
#include <stdio.h>
#include <stdlib.h> //이게 1번일 가능성이 높고
int main()
{
int *ip;
double *dp;
ip = (int *)malloc(sizeof(int)); //이게 2번일 가능성이 높고
dp = (double *)malloc(sizeof(double)); //이게 3번일 가능성이 높음
*ip = 10;
*dp = 3.4;
printf("정수형으로 사용 : %d \n", *ip);
printf("실수형으로 사용 : %lf \n", *dp);
free (ip);
free (dp);
return 0;
}
3. student 구조체 만들기
#include <stdio.h>
typedef struct
{
int snum;
char *name;
char *major;
double score;
}student;
int main()
{
student s1;
s1.snum = 100;
s1.name = "이름";
s1.major = "소프트웨어 개발 전공";
s1.score = 4.5;
printf ("학번 : %d 이름 : %s 학과 : %s 학점 : %0.2lf\n", s1.snum, s1.name, s1.major,s1.score);
}
4. 1차원 배열과 포인터를 이용하여 다음 출력 같이 코딩
array forward : 1 2 3 4 5 6 7 8 9 10
array backword : 10 9 8 7 6 5 4 3 2 1
#include <stdio.h>
#define MAX_SIZE 10
int main(void)
{
int ary[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *pWalk;
int *pEnd;
printf("Array forward : ");
for (pWalk = ary, pEnd = ary + MAX_SIZE; pWalk < pEnd; pWalk++)
printf("%3d", *pWalk);
printf("\n");
printf("Array backward: ");
for (pWalk = pEnd -1; pWalk >= ary; pWalk--)
printf("%3d", *pWalk);
printf("\n");
return 0;
}


최근 덧글