若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
指標的慣念可以看 C語言: 超好懂的指標,初學者請進
Pointer concepts:
2. Introduction to C Pointers練習一:基本語法
設計一個C語言程式來呈現指標的語法,例如宣告、取址、取值等。
Exercise 1: Basic Syntax
練習一參考解法:
Exercise 1 solution:
練習二:動態記憶體配置
撰寫用 malloc() 與 free() 來配置記憶體的程式。
Design a C program to demonstrate the basic syntax of pointer. Such as declaration, address and value.
練習一參考解法:
Exercise 1 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /* Pointer Basic Syntax Author: Holan */ #include <stdio.h> #include <stdlib.h> int main() { int n = 50; // declaration int *ip; // assignment ip = &n; printf("The value of &n:%X\n", &n); printf("The value of n:%i\n", n); printf("The value of &ip:%X\n", &ip); printf("The value of ip:%X\n", ip); printf("The value of *ip:%i\n", *ip); return 0; } |
練習二:動態記憶體配置
撰寫用 malloc() 與 free() 來配置記憶體的程式。
Exercise 2: Dynamic Memory Allocation
Write a C program to allocate memory.
練習二參考解法:
Exercise 2 solution:
練習三:兩數相乘
使用指標的方式,將兩個數字相乘。
Write a C program to allocate memory.
練習二參考解法:
Exercise 2 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /* Dynamic memory allocation Author: Holan */ #include <stdio.h> #include <stdlib.h> int main() { int n; int *pI; printf("How many integers? "); scanf("%d", &n); // using malloc to allocate memory pI = (int *) malloc(n*sizeof(int)); if(pI == NULL) // if fail to allocate { printf("Couldn't allocate memory\n"); return 0; // exit } // Releasing the memory allocated by malloc free(pI); return 0; } |
練習三:兩數相乘
使用指標的方式,將兩個數字相乘。
Exercise 3: Multiplying two numbers
Multiplying two numbers with pointers.
練習三參考解法:
Exercise 3 solution:
練習四:指標與陣列
使用指標的語法來取得整數陣列的元素。
Multiplying two numbers with pointers.
練習三參考解法:
Exercise 3 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /* Multiplying two numbers with pointers Author: Holan */ #include <stdio.h> #include <stdlib.h> int main() { int n1, n2; int *pn1, *pn2, product; printf("Enter num1:"); scanf("%d", &n1); printf("Enter num2:"); scanf("%d", &n2); pn1 = &n1; pn2 = &n2; product = *pn1 * *pn2; printf("The product of %d and %d is %d\n", *pn1, *pn2, product); return 0; } |
練習四:指標與陣列
使用指標的語法來取得整數陣列的元素。
Exercise 4: Pointer and array
Using a pointer to access the elements of an integer array.
練習四參考解法:
Exercise 4 solution:
練習五:指標運算
在練習四時,使用到指標與整數相加的運算。而本題請使用遞增與遞減來達成與練習四一樣的功能。
練習四參考解法:
Exercise 4 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* Pointer and array Author: Holan */ #include <stdio.h> #include <stdlib.h> #define N 5 int main() { int a[N] = { 2, 3, 5, 7, 11}; int *pI = a; for(int i = 0; i < N; i++) printf("a[%d] = %d\t", i, *(pI + i)); return 0; } |
練習五:指標運算
在練習四時,使用到指標與整數相加的運算。而本題請使用遞增與遞減來達成與練習四一樣的功能。
Exercise 5: Pointer Arithmetic
In exercise 4, the operation: "a pointer plus a integer" is used. Could we use pointer increment and decrement to do exercise 4?
這篇連結文章可以幫助瞭解指標運算。
Here is a tutorial: "How pointer arithmetic works?"
練習五參考解法:
Exercise 5 solution:
練習六:字串長度
使用指標語法來求出所輸入字串的長度。
In exercise 4, the operation: "a pointer plus a integer" is used. Could we use pointer increment and decrement to do exercise 4?
這篇連結文章可以幫助瞭解指標運算。
Here is a tutorial: "How pointer arithmetic works?"
練習五參考解法:
Exercise 5 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | /* Pointer Arithmetic: increment and decrement Author: Holan */ #include <stdio.h> #include <stdlib.h> #define N 5 int main() { int a[N] = { 2, 3, 5, 7, 11}; int *pI = a; printf("Pointer increment:"); // pointer increment for(int i = 0; i < N; i++) printf("a[%d] = %d\t", i, *(pI++)); printf("\n\nPointer decrement:"); // pointer decrement for(int i = N - 1; i >= 0; i--) printf("a[%d] = %d\t", i, *(--pI)); return 0; } |
練習六:字串長度
使用指標語法來求出所輸入字串的長度。
Exercise 6: The length of a string
Using pointer to calculate the length of a user input string.
練習六參考解法:
Exercise 6 solution:
Using pointer to calculate the length of a user input string.
練習六參考解法:
Exercise 6 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /* Length of a string Author: Holan */ #include <stdio.h> #include <stdlib.h> int main() { char str[100]; int len = 0; printf("Enter a string:"); // Input a string with whitespaces scanf("%[^\n]s", str); char *pC = str; while(*pC != '\0') { len++; pC++; } printf("The length of the given string[%s] is:%d", str, len); return 0; } |
練習七:交換兩數
使用 call by reference 的方式,設計一個可以交換兩數的函式。
使用 call by reference 的方式,設計一個可以交換兩數的函式。
Exercise 7: Swap two numbers
Using call by reference to design a function that swaps two numbers.
練習七參考解法:
Exercise 7 solution:
Using call by reference to design a function that swaps two numbers.
練習七參考解法:
Exercise 7 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /* Swap two numbers Author: Holan */ #include <stdio.h> #include <stdlib.h> void swapV(double *x, double *y); int main() { double n1, n2; printf("Enter number 1:"); scanf("%lf", &n1); printf("Enter number 2:"); scanf("%lf", &n2); printf("Before swap n1:%lf, n2:%lf", n1, n2); swapV(&n1, &n2); printf("\nAfter swap n1:%f, n2:%f", n1, n2); return 0; } void swapV(double *x, double *y) { double t = *x; *x = *y; *y = t; } |
練習八:字串合併
使用指標語法來合併兩字串。
使用指標語法來合併兩字串。
Exercise 8: String concatenation
Using pointer to concatenate two strings.
練習八參考解法:
Exercise 8 solution:
Using pointer to concatenate two strings.
練習八參考解法:
Exercise 8 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | /* String concatenation Author: Holan */ #include <stdio.h> #include <stdlib.h> #define LEN 256 int main() { char str1[LEN]; char str2[LEN]; char str3[LEN*2]; printf("Enter str1:"); scanf("%[^\n]%*c", str1); printf("Enter str2:"); scanf("%[^\n]%*c", str2); char *pC = str1; char *pStr = str3; while(*pC) { *pStr = *pC; pC++; pStr++; } pC = str2; while(*pC) { *pStr = *pC; pC++; pStr++; } // end with '\0' *pStr = '\0'; printf("Str1:%s, Str2:%s, Str3:%s", str1, str2, str3); return 0; } |
請撰寫一程式來練習空指標的語法。
Exercise 9: null pointer
Please refer to this tutorial: NULL pointer in C.
Design a C program to practice the null pointer syntax.
練習九參考解法:
Exercise 9 solution:
Please refer to this tutorial: NULL pointer in C.
Design a C program to practice the null pointer syntax.
練習九參考解法:
Exercise 9 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* NULL pointer Author: Holan */ #include <stdio.h> #include <stdlib.h> int main() { int *iP = NULL; if(iP == NULL) { printf("The value of iP is %u", iP); // Couldn't access to *iP // The following code will crush printf("The value of *iP is %d", *iP); } else { printf("The value of iP is %u", iP); printf("The value of *iP is %d", *iP); } return 0; } |
練習十:函式指標
可參考此篇文章:[C語言] function pointer介紹來了解 function pointer。
可參考此篇文章:[C語言] function pointer介紹來了解 function pointer。
請設計一程式來練習 function pointer,例如兩數字的加、減、乘、除等運算。
Exercise 10: Function Pointer
Please refer to this tutorial: Function pointers in C.
Design a C program to practice function pointer. For example, addition, subtraction, multiplication and division of two numbers.
練習十參考解法:
Exercise 10 solution:
Please refer to this tutorial: Function pointers in C.
Design a C program to practice function pointer. For example, addition, subtraction, multiplication and division of two numbers.
練習十參考解法:
Exercise 10 solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /* Function pointers Author: Holan */ #include <stdio.h> #include <stdlib.h> double add(double n1, double n2); double sub(double n1, double n2); double mul(double n1, double n2); double divi(double n1, double n2); int main() { double a = 3.44, b = 9.999; double (*op)(double, double) = add; printf("The addition of %f and %f is %f\n", a, b, op(a, b)); op = sub; printf("The subtraction of %f and %f is %f\n", a, b, op(a, b)); op = mul; printf("The multiplication of %f and %f is %f\n", a, b, op(a, b)); op = divi; printf("The division of %f and %f is %f\n", a, b, op(a, b)); return 0; } double add(double a, double b) { return a + b;} double sub(double a, double b) { return a - b;} double mul(double a, double b) { return a * b;} double divi(double a, double b) { return a / b;} |