diff --git a/HW2_three files/README.md b/HW2_three files/README.md index 2353bc81ae9318cc0dcfff7107fc8ab0d8d3ec0d..acd2c48c59f1a2f2a3e11dc66bb30f7c005692a1 100644 --- a/HW2_three files/README.md +++ b/HW2_three files/README.md @@ -1,5 +1,6 @@ # 실전코딩2 HW2 -- gcc + +# HW2_GCC # CPP - include file diff --git a/HW3/README.md b/HW3/README.md index 4f81e588f4aa8be4182929b8455937924035397a..423a69683f0b1fd794415dc7d4386390efda54bf 100644 --- a/HW3/README.md +++ b/HW3/README.md @@ -1,5 +1,7 @@ # 실전코딩2 HW3 +## HW3_GCC + # Difference between Compiler and interpreter - compiler 1. 프로그래밍 언어를 Runtime 이전에 기계어로 해석 diff --git a/HW5_GPIO/README.md b/HW4_GPIO/README.md similarity index 95% rename from HW5_GPIO/README.md rename to HW4_GPIO/README.md index 4909460d43bcbe2a452c04c936569b760e1f50b0..3ef490374e2d092265c7683c3f531ccf5e625ef3 100644 --- a/HW5_GPIO/README.md +++ b/HW4_GPIO/README.md @@ -1,6 +1,6 @@ # 실전코딩 2 -# HW5_GPIO 연결 +## HW4_GPIO 연결 ## HW 사진1 -  diff --git a/HW5_GPIO/image/1.jpg b/HW4_GPIO/image/1.jpg similarity index 100% rename from HW5_GPIO/image/1.jpg rename to HW4_GPIO/image/1.jpg diff --git a/HW5_GPIO/image/2.jpg b/HW4_GPIO/image/2.jpg similarity index 100% rename from HW5_GPIO/image/2.jpg rename to HW4_GPIO/image/2.jpg diff --git a/HW5_GPIO/image/3.jpg b/HW4_GPIO/image/3.jpg similarity index 100% rename from HW5_GPIO/image/3.jpg rename to HW4_GPIO/image/3.jpg diff --git a/HW5_GPIO/image/test.c b/HW4_GPIO/image/test.c similarity index 100% rename from HW5_GPIO/image/test.c rename to HW4_GPIO/image/test.c diff --git a/HW5_Pointer/README.md b/HW5_Pointer/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f6734a5bdce75f5d595be8fdeb1a0946e03a0f89 --- /dev/null +++ b/HW5_Pointer/README.md @@ -0,0 +1,134 @@ +# 실전코딩 2 + +## HW5_Pointer + +## 포인터 개념 +- 포인터 변수는 메모리 주소를 저장한다. +- 포인터는 특정 변수 자체가 존재하는 메모리 주소의 값을 가진다. + +<그림 1> +다음 사진처럼 a를 이용해서 5를 찾을 수 있고 포인터 변수 b를 이용해서 5를 찾을 수 있다. + +<그림 2> +int a = 5; 라고 변수를 할당하면 메모리 주소상에서 <그림 2>처럼 기록된다. +- int - 4byte +- long long - 8byte +- void - 1byte + +## 포인터 주의사항 +<그림 3> +위와 같은 코드는 부적절하다. +- 포인터는 컴퓨터 시스템의 특정 메모리에 바로 접근할 수 있기 때문에 기존에 존재하던 중요한 메모리 영역에 접근하지 않도록 한다. + +## 포인터 관련 연산자 +- 주소 연선자(&) + - 변수 앞에 붙어서 변수의 메모리 시작 주소값을 구한다. + +- 포인터(*) + - 포인터 변수를 선언할 때 사용한다. + +- 간접 참조 연산자(*) + - 선언된 포인터 변수가 가리키는 변수를 구한다. + +## 포인터 배열 +``` C code +#include <stdio.h> + +int main() +{ + int a[10], b[30], c[40]; + int * t[3]; + t[0] = a; + t[1] = b; + t[2] = c; + t[1][35] = 200; + printf("%d\n", b[35]); + for (int i = 0 ; i < 40 ; i++) + printf("%d ", c[i]); +} +``` +- 다음은 test4.c의 코드이고, 세개의 배열을 하나의 포인터 배열로 처리함 + - t[0] -> a[10] + - t[1] -> b[30] + - t[2] -> c[40] + +## 함수 포인터 +- 함수 포인터는 특정한 함수의 반환 자료형을 지정하는 방식으로 선언할 수 있다. +- 함수 포인터를 사용하면 형태가 같은 서로 다른 기능의 함수를 선택적으로 사용할 수 있다. + +사용법: 반환 자료형(*이름)(매개변수) = 함수명; +- 예시코드 test5.c + +```C code +#include <stdio.h> +int f10(int a) +{ return a+10; } +int f5(int a) +{ return a+5; } +int f1(int a) +{ return a+1; } +int f22(int a) +{ return a+22; } + + + +int main() +{ + int a = 200; + int (*f[4])(int) = {f10, f5, f1, f22}; + printf("%d\n", f[2](10)); + +} +``` +f[4]의 포인터 배열에 각각 f10, f5, f1, f22의 함수를 넣어 놓고 인덱스 2에 있는 f1함수에 10의 매개변수를 전달하여 리턴값 11을 받음 + +## 코드 분석 +```C code +#include <stdio.h> + +void func(int *p) +{ + int *q = p; + *q = 100; + q++; + *q = 200; +} + +int main() +{ + int a = 1; + int b = 2; + int *pa, *pb; + long long pp; + pa = &a; + pb = &b; + pp = (long long) pa; + printf("%d %d\n", a, b); + func(&b); + printf("%d %d\n", *pa, *pb); + printf("%d %d %d\n", pa, pb, *(int *) pp); + printf("%d %d\n", a, b); + printf("%d, %d, %d, %d\n", a, &a, *(&a), *(int *)(long long)(&a) ); +} +``` +- 다음 코드에서는 Memory주소를 받아서 새로운 포인터로 가리키게 만들면 쉽게 기존의 다른 포인터들의 값을 참조할 수 있다. + - 해킹의 위험이 존재 + +```C code +#include <stdio.h> + +int main() +{ + int a[10] = { 0, 0, 0, 1, 4, 5, 6, 7, 8, 9}; + // void *b; + long long *c; + // b = &a[0]; + c = (long long *)a; + printf("%d, %d\n", a, *a); + printf("%x, %llx\n", c, *c); + printf("%x, %llx\n", c+1, *(c+1)); + float kk = 0.75f; + printf("%f : %d, %x \n", kk, *(int *)&kk, *(int *)&kk); +} +``` +- 다음 코드에서는 int형 배열a를 long long형으로 캐스팅 하고 float형을 부동소수점으로 표현할 수 있다. \ No newline at end of file diff --git a/HW5_Pointer/a.exe b/HW5_Pointer/a.exe new file mode 100644 index 0000000000000000000000000000000000000000..8b5a5501f5196158dfc287b300298c763ae79d95 Binary files /dev/null and b/HW5_Pointer/a.exe differ diff --git a/HW5_Pointer/image/1.PNG b/HW5_Pointer/image/1.PNG new file mode 100644 index 0000000000000000000000000000000000000000..ca013ee8650c5d21d44a31bd6d707c6182cc44fb Binary files /dev/null and b/HW5_Pointer/image/1.PNG differ diff --git a/HW5_Pointer/image/2.PNG b/HW5_Pointer/image/2.PNG new file mode 100644 index 0000000000000000000000000000000000000000..2f3e68c113a9857f51cc142e99e2a2e038a95e17 Binary files /dev/null and b/HW5_Pointer/image/2.PNG differ diff --git a/HW5_Pointer/image/3.PNG b/HW5_Pointer/image/3.PNG new file mode 100644 index 0000000000000000000000000000000000000000..f4cb4f549d09007659063acd623d32da2b010098 Binary files /dev/null and b/HW5_Pointer/image/3.PNG differ diff --git a/HW5_Pointer/test0-hack-point.c b/HW5_Pointer/test0-hack-point.c new file mode 100644 index 0000000000000000000000000000000000000000..73d027a18b2bc2d888ef108bad64e6b590c05b15 --- /dev/null +++ b/HW5_Pointer/test0-hack-point.c @@ -0,0 +1,26 @@ +#include <stdio.h> + +void func(int *p) +{ + int *q = p; + *q = 100; + q++; + *q = 200; +} + +int main() +{ + int a = 1; + int b = 2; + int *pa, *pb; + long long pp; + pa = &a; + pb = &b; + pp = (long long) pa; + printf("%d %d\n", a, b); + func(&b); + printf("%d %d\n", *pa, *pb); + printf("%d %d %d\n", pa, pb, *(int *) pp); + printf("%d %d\n", a, b); + printf("%d, %d, %d, %d\n", a, &a, *(&a), *(int *)(long long)(&a) ); +} diff --git a/HW5_Pointer/test1-type-conv.c b/HW5_Pointer/test1-type-conv.c new file mode 100644 index 0000000000000000000000000000000000000000..2f957673599233e2c1fd78d790227931482cf7cd --- /dev/null +++ b/HW5_Pointer/test1-type-conv.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int main() +{ + int a[10] = { 0, 0, 0, 1, 4, 5, 6, 7, 8, 9}; + // void *b; + long long *c; + // b = &a[0]; + c = (long long *)a; + printf("%d, %d\n", a, *a); + printf("%x, %llx\n", c, *c); + printf("%x, %llx\n", c+1, *(c+1)); + float kk = 0.75f; + printf("%f : %d, %x \n", kk, *(int *)&kk, *(int *)&kk); +} diff --git a/HW5_Pointer/test2-size-change.c b/HW5_Pointer/test2-size-change.c new file mode 100644 index 0000000000000000000000000000000000000000..884234b135e101a832da7ccf8d08df3945de6903 --- /dev/null +++ b/HW5_Pointer/test2-size-change.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +int func(const int a) +{ + int b[a]; + b[0] = 200; + printf("%d\n", b[0]); +} + +int main() +{ + int a = 1; + int b = 2; + void *v = &a; + printf("v add %d \n", v); + v++; + printf("v add %d \n", v); + printf("%d %d\n", a, b); + func(10); + printf("%d %d\n", a, b); +} diff --git a/HW5_Pointer/test3-const-but-change.c b/HW5_Pointer/test3-const-but-change.c new file mode 100644 index 0000000000000000000000000000000000000000..78dd16e0bf51dab275ffaee2647b9cb9551380b8 --- /dev/null +++ b/HW5_Pointer/test3-const-but-change.c @@ -0,0 +1,11 @@ +#include <stdio.h> + +int main() +{ + const int a = 99; + int const *b = &a; + + *(int *)b = 200; + + printf("%d \n", a); +} diff --git a/HW5_Pointer/test3.c b/HW5_Pointer/test3.c new file mode 100644 index 0000000000000000000000000000000000000000..9992c2d28e5f7aa29ee99eaa5e587e260632d8c0 --- /dev/null +++ b/HW5_Pointer/test3.c @@ -0,0 +1,17 @@ +#include <stdio.h> +void set_elmt(int *a) +{ + a[0] = 3; +} + +int main() +{ + const int a = 10; + int *b = &a; + printf("%d \n", a); + set_elmt(b); + printf("%d \n", a); + int * const c = b; + *c = 100; + printf("%d \n", a); +} diff --git a/HW5_Pointer/test4.c b/HW5_Pointer/test4.c new file mode 100644 index 0000000000000000000000000000000000000000..73024235c51281a1d6b914248e1380fbd5a5ca19 --- /dev/null +++ b/HW5_Pointer/test4.c @@ -0,0 +1,14 @@ +#include <stdio.h> + +int main() +{ + int a[10], b[30], c[40]; + int * t[3]; + t[0] = a; + t[1] = b; + t[2] = c; + t[1][35] = 200; + printf("%d\n", b[35]); + for (int i = 0 ; i < 40 ; i++) + printf("%d ", c[i]); +} diff --git a/HW5_Pointer/test5.c b/HW5_Pointer/test5.c new file mode 100644 index 0000000000000000000000000000000000000000..56ad286e28428d9ef61b44a928d059895e54a100 --- /dev/null +++ b/HW5_Pointer/test5.c @@ -0,0 +1,19 @@ +#include <stdio.h> +int f10(int a) +{ return a+10; } +int f5(int a) +{ return a+5; } +int f1(int a) +{ return a+1; } +int f22(int a) +{ return a+22; } + + + +int main() +{ + int a = 200; + int (*f[4])(int) = {f10, f5, f1, f22}; + printf("%d\n", f[2](10)); + +} diff --git a/HW5_Pointer/test6.c b/HW5_Pointer/test6.c new file mode 100644 index 0000000000000000000000000000000000000000..98c815d3b9470184c5d183403b470b6dddd34d84 --- /dev/null +++ b/HW5_Pointer/test6.c @@ -0,0 +1,24 @@ +#include <stdio.h> + +int f10(int a) +{ return a+10; } +int f5(int a) +{ return a+5; } +int f1(int a) +{ return a+1; } +int f22(int a) +{ return a+22; } + + + +int main() +{ + double (*f[10])(int const *a, double (*g[10])(double h)); + int (*q)(int, int, double); + + int a = 200; + int (*ff)(int); + ff = f10; + printf("%d\n", ff(a)); + +} diff --git a/HW5_Pointer/test7.c b/HW5_Pointer/test7.c new file mode 100644 index 0000000000000000000000000000000000000000..ab39df523fd47e2a229a6f46331062227fa9ae62 --- /dev/null +++ b/HW5_Pointer/test7.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +float f_f() {return 100.0; } +int f_i() {return 100; } + + +int main() +{ + int (*f)(void); + float (*ff)(void); + f = f_i; + printf("%d\n", f()); + ff = f_f; + printf("%f\n", ff()); +}