From e73b06afa523ab3b37731c63856e632c1706e8a5 Mon Sep 17 00:00:00 2001 From: flykhan Date: Sat, 22 Jul 2023 11:37:14 +0800 Subject: [PATCH] =?UTF-8?q?01=5F=E5=B5=8C=E5=85=A5=E5=BC=8F=E9=AB=98?= =?UTF-8?q?=E7=BA=A7C=E8=AF=AD=E8=A8=80=E9=98=B6=E6=AE=B5=E8=80=83?= =?UTF-8?q?=E6=A0=B8=E9=A2=98=E7=9B=AE=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/d1.c | 25 +++++++++++++++++++++++++ test/d2.c | 25 +++++++++++++++++++++++++ test/t1_11.c | 13 +++++++++++++ test/t1_4.c | 15 +++++++++++++++ test/t2_1_4.c | 18 ++++++++++++++++++ test/t3_3.c | 17 +++++++++++++++++ 6 files changed, 113 insertions(+) create mode 100644 test/d1.c create mode 100644 test/d2.c create mode 100644 test/t1_11.c create mode 100644 test/t1_4.c create mode 100644 test/t2_1_4.c create mode 100644 test/t3_3.c diff --git a/test/d1.c b/test/d1.c new file mode 100644 index 0000000..3977e13 --- /dev/null +++ b/test/d1.c @@ -0,0 +1,25 @@ +#include + +// 这种方式只能交换指针指向的值,不能交换指针本身,而且无法改变实参的值 +void swap(int *piA, int *piB) +{ + printf("first iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", piA, piB, *piA, *piB); + + int *piC; + piC = piA; + piA = piB; + piB = piC; + printf("after iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", piA, piB, *piA, *piB); +} + +int main() +{ + int iA = 100, iB = 200; + printf("main first iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", &iA, &iB, iA, iB); + + int *piA = &iA, *piB = &iB; + swap(piA, piB); + printf("main after iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", &iA, &iB, iA, iB); + + return 0; +} \ No newline at end of file diff --git a/test/d2.c b/test/d2.c new file mode 100644 index 0000000..966e270 --- /dev/null +++ b/test/d2.c @@ -0,0 +1,25 @@ +#include + +// 会交换实参的值 +void swap(int *piA, int *piB) +{ + // printf("first iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", piA, piB, *piA, *piB); + + int piC; + piC = *piA; + *piA = *piB; + *piB = piC; + // printf("after iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", piA, piB, *piA, *piB); +} + +int main() +{ + int iA = 100, iB = 200; + printf("first iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", &iA, &iB, iA, iB); + + int *piA = &iA, *piB = &iB; + swap(piA, piB); + printf("after iA-p: %p, iB-p: %p\n iA: %d,iB: %d\n", &iA, &iB, iA, iB); + + return 0; +} \ No newline at end of file diff --git a/test/t1_11.c b/test/t1_11.c new file mode 100644 index 0000000..48bdda6 --- /dev/null +++ b/test/t1_11.c @@ -0,0 +1,13 @@ +#include + +#define ADD(x, y) (x + y) /*加*/ +#define SUB(x, y) (x) - (y) /*减*/ +#define MUL(x, y) (x * y) /*乘*/ + +int main() +{ + unsigned int uiA; + uiA = MUL(ADD(9, 2), SUB(6, 3)); // ((9+2)*(6)-(3)) = 63 + printf("uiA = %d\n", uiA); + return 0; +} \ No newline at end of file diff --git a/test/t1_4.c b/test/t1_4.c new file mode 100644 index 0000000..d06a21d --- /dev/null +++ b/test/t1_4.c @@ -0,0 +1,15 @@ +#include +int main() +{ + int i; + int sum = 1; + for (i = 1; i <= 10; i++) + { + sum += i; // 加完之后才判断是否为5,因此sum会加上5 + if (i == 5) + continue; + // printf("i = %d, sum=%d\n", i, sum); + } + printf("sum=%d\n", sum); + return 0; +} \ No newline at end of file diff --git a/test/t2_1_4.c b/test/t2_1_4.c new file mode 100644 index 0000000..623d508 --- /dev/null +++ b/test/t2_1_4.c @@ -0,0 +1,18 @@ +#include + +int main() +{ + unsigned int uiCount = 0; + for (; uiCount <= 10; uiCount++) + { + printf("uiCount=%u\n", uiCount); + + if (uiCount == 5) + { + break; + } + // printf("uiCount=%u\n", uiCount); + } + // printf("uiCount=%u\n", uiCount); + return 0; +} \ No newline at end of file diff --git a/test/t3_3.c b/test/t3_3.c new file mode 100644 index 0000000..75c6532 --- /dev/null +++ b/test/t3_3.c @@ -0,0 +1,17 @@ +void func(char aver[100]) +{ + char str[] = "hello world"; + char *p = str; + char n = 10; + void *p1 = malloc(100); + printf("size1=[%d]\n", sizeof(str)); + printf("size2=[%d]\n", sizeof(p)); + printf("size3=[%d]\n", sizeof(n)); + printf("size4=[%d]\n", sizeof(aver)); + printf("size5=[%d]\n", sizeof(p1)); +} +int main() +{ + char str[100]; + func(str); +} \ No newline at end of file