From 72746c972e9e83b6cc3e906c7c3494e27a0ff696 Mon Sep 17 00:00:00 2001 From: Jieun <yjeka@ajou.ac.kr> Date: Fri, 14 Aug 2020 00:48:35 +0900 Subject: [PATCH] Add pointer practice --- pointer/pointer_practice/1.ptr_var.c | 20 +++++++++++ pointer/pointer_practice/2.constPtrVar.c | 21 +++++++++++ pointer/pointer_practice/4.ptr_op1.c | 19 ++++++++++ pointer/pointer_practice/5.ptr_op2.c | 16 +++++++++ pointer/pointer_practice/6.ptrNarray1.c | 29 +++++++++++++++ pointer/pointer_practice/7.ptrNarray2.c | 41 +++++++++++++++++++++ pointer/pointer_practice/8.functionPtr1.c | 43 +++++++++++++++++++++++ 7 files changed, 189 insertions(+) create mode 100644 pointer/pointer_practice/1.ptr_var.c create mode 100644 pointer/pointer_practice/2.constPtrVar.c create mode 100644 pointer/pointer_practice/4.ptr_op1.c create mode 100644 pointer/pointer_practice/5.ptr_op2.c create mode 100644 pointer/pointer_practice/6.ptrNarray1.c create mode 100644 pointer/pointer_practice/7.ptrNarray2.c create mode 100644 pointer/pointer_practice/8.functionPtr1.c diff --git a/pointer/pointer_practice/1.ptr_var.c b/pointer/pointer_practice/1.ptr_var.c new file mode 100644 index 0000000..52d45e1 --- /dev/null +++ b/pointer/pointer_practice/1.ptr_var.c @@ -0,0 +1,20 @@ +#include <stdio.h> + +int main(void) +{ + short n1=10, n2=20; + int n3=0x10203040; + short *p; //n1의 타입을 적는다. + p = &n1; + printf("n1:%d, *p:%d\n", n1, *p); + *p = 100; + printf("n1:%d, *p:%d\n", n1, *p); + (*p)++; + printf("n1:%d, *p:%d\n", n1, *p); + p = &n2; + printf("n2:%d, *p:%d\n", n2, *p); + p = &n3; + printf("n3:%x, *p:%x\n", n3, *p); + printf("n3:%x, *p:%x\n", n3, *(p+1)); + return 0; +} diff --git a/pointer/pointer_practice/2.constPtrVar.c b/pointer/pointer_practice/2.constPtrVar.c new file mode 100644 index 0000000..ba41d10 --- /dev/null +++ b/pointer/pointer_practice/2.constPtrVar.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +int main(void) { + int n1=10, n2=20, n3=30; + int * p1=&n1; + const int * p2=&n1; //int를 상수화하겠다. + int * const p3=&n1; //p3를 상수화하겠다. p3의 주소값을 못바꾸게 하겠다. + const int * const p3=&n1; + + (*p1)++; + (*p2)++; //p2의 내용물을 변경할 수 없다. + (*p3)++; + (*p4)++; //p4 변경 x + + p1 = &n2; + p2 = &n2; + p3 = &n2; //p3를 변경할 수 없다. + p4 = &n2; //p4 변경 x + + return 0; +} diff --git a/pointer/pointer_practice/4.ptr_op1.c b/pointer/pointer_practice/4.ptr_op1.c new file mode 100644 index 0000000..9f9f8ea --- /dev/null +++ b/pointer/pointer_practice/4.ptr_op1.c @@ -0,0 +1,19 @@ +#include <stdio.h> + +int main(void) +{ + short data[] = {1, 2, 3, 4, 5}; + short *p; + p = &data[0]; //p = data; + printf("data[0]:%d, *p:%d\n", data[0], *p); + p++; + printf("data[1]:%d, *p:%d\n", data[1], *p); + p += 2; + printf("data[3]:%d, *p:%d\n", data[3], *p); + p += -2; + printf("data[1]:%d, *p:%d\n", data[1], *p); + + //p *= 2; + //p /= 2; + return 0; +} diff --git a/pointer/pointer_practice/5.ptr_op2.c b/pointer/pointer_practice/5.ptr_op2.c new file mode 100644 index 0000000..e5b8757 --- /dev/null +++ b/pointer/pointer_practice/5.ptr_op2.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +int main(void) +{ + short data[] = {1, 2, 3, 4, 5}; + short *p1=&data[0], *p2=&data[4]; + + printf("p1:%p, p2:%p\n", p1, p2); + printf("p1+4:%p\n", p1 + 4); + printf("p2-4:%p\n", p2 - 4); + printf("p2-p1:%d\n", p2 - p1); //주소 offset + //printf("p1+p2:%p\n", p1 + p2); + //printf("p1*p2:%p\n", p1 * p2); + //printf("p1/p2:%p\n", p1 / p2); + return 0; +} diff --git a/pointer/pointer_practice/6.ptrNarray1.c b/pointer/pointer_practice/6.ptrNarray1.c new file mode 100644 index 0000000..4482e54 --- /dev/null +++ b/pointer/pointer_practice/6.ptrNarray1.c @@ -0,0 +1,29 @@ +#include <stdio.h> + +int main(void) +{ + short arr[5] = {1, 2, 3, 4, 5}; + short *p; //p=arr; p=&arr[0]; short * const arr; + int i; + + p = &arr[0]; + printf("%d,%d\n", p[1], p[3]); + + p = &arr[1]; + printf("%d,%d\n", p[1], p[3]); + +#if 0 + for(i=0, p=arr; i<5; i++) { + printf("%d,%d\n", arr[i], p[i]); + printf("%d,%d\n", *(arr+i), *(p+i)); + } +#endif + for(i=0, p=arr; i<5; i++, p++) { + printf("%d,%d\n", arr[i], *p); + } + for(i=0, p=arr; i<5; i++, arr++) { + printf("%d,%d\n", p[i], *arr); + } + + return 0; +} diff --git a/pointer/pointer_practice/7.ptrNarray2.c b/pointer/pointer_practice/7.ptrNarray2.c new file mode 100644 index 0000000..9a5f197 --- /dev/null +++ b/pointer/pointer_practice/7.ptrNarray2.c @@ -0,0 +1,41 @@ +#include <stdio.h> + +void printData_1(const short *data)//값 변경을 못하게 할 때 +{ + int i; + + for(i=0; i<5; i++) { + printf("%d,%d\n", data[i], *(data+i)); + //data[i] = 9999; //short * data일때 + } +} + +void printData_2(short data[5]) +{ + int i; + + for(i=0; i<5; i++) { + printf("%d,%d\n", data[i], *(data+i)); + } +} + +void updateData(short *data) +{ + int i; + for(i=0; i<5; i++) data[i] += 10; +} + +int main(void) +{ + short arr[5] = {1, 2, 3, 4, 5}; + + printf("-------------------------\n"); + printData_1(arr); + printf("-------------------------\n"); + printData_2(arr); + printf("-------------------------\n"); + updateData(arr); + printData_2(arr); + + return 0; +} diff --git a/pointer/pointer_practice/8.functionPtr1.c b/pointer/pointer_practice/8.functionPtr1.c new file mode 100644 index 0000000..574013e --- /dev/null +++ b/pointer/pointer_practice/8.functionPtr1.c @@ -0,0 +1,43 @@ +#include <stdio.h> + +int add(int d1, int d2) { + return d1+d2; +} +int sub(int d1, int d2) { + return d1-d2; +} +int mul(int d1, int d2) { + return d1*d2; +} +int div(int d1, int d2) { + if(d2) return d1/d2; + else { printf("Zero Dived Error!!\n"); return 0; } +} + +int main(void) +{ + int no, rst; + //함수 포인터 + int (*fp) (int, int); //add의 타입을 적어야 한다. //int(int, int) *fp; + //(*fp) : 포인터 변수이고 포인터 변수의 타입은 int(int,int) + + while(1) { + do { + printf("\nSelect(1.add, 2.sub, 3.mul, 4.div, 0.quit) => "); + scanf("%d", &no); + } while(no<0 || no>4); + if(no == 0) break; + + switch(no){ + case 1: fp = &add; break; + case 2: fp = sub; break; + case 3: fp = mul; break; + case 4: fp = ÷ break; + } + + rst = fp(10, 20); + printf("rst => %d\n", rst); + } + + return 0; +} -- GitLab