/* * @Author: huanjiang-lab vibration@zju.edu.cn * @Date: 2023-11-10 13:51:34 * @LastEditors: huanjiang-lab vibration@zju.edu.cn * @LastEditTime: 2023-11-27 09:19:51 * @FilePath: \c-learn\zhizhen.c * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ // C program to demonstrate that we can change // local values of one function in another using pointers. #include <stdio.h> #define MAX 10 void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } void fun(int *height, int *age) { int n = 10; *height = n * 10; *age = n * 2000; } int *createArray(int size) { int i; int *arr = (int *)(malloc(sizeof(int) * size)); for (i = 0; i < size; i++) { arr[i] = i * i; } return arr; } int *sumArray(int arr) { return 0; } int add(int x, int y) { return x + y; } int substract(int x, int y) { return x - y; } int multiply(int x, int y) { return x * y; } int divide(int x, int y) { return x / y; } int module(int x, int y) { return x % y; } int sum2DArray(int **arr, int *start, int *end) { int sum = 0; // int *p = *arr; int *p = NULL; p = start; while (p < end) { sum += *p; p++; } return sum; } typedef int (*operation_fun)(int, int); void main() { int num = 0; int (*ptr_add)(int, int) = add; void (*ptr_swap)(int, int) = swap; int a = 100, b = 200; int sum = ptr_add(a, b); ptr_swap(&a, &b); printf("the result is %d\n", sum); printf("after swap a is %d,b is %d\n", a, b); operation_fun operations[] = { &add, &substract, &multiply, ÷, &module}; // int (*operations[])(int, int) = {add, substract, multiply, divide, module}; // int (*operations[])(int, int) = {&add, &substract, &multiply, ÷, &module}; char *arr[] = {"add", "substract", "multiply", "divide", "module"}; int result = 0; int i, j; for (i = 0; i < 5; i++) { result = operations[i](10, 20); printf("%s of two number result is %d\n", arr[i], result); } int arr1[3][4] = {0}; int arr2[3][4] = {0}; int arr3[3][4] = {0}; for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { arr1[i][j] = i + j; arr2[i][j] = i - j; arr3[i][j] = i * j; } } int *ptr_arr_start = &arr2; int *ptr_arr_end = &arr2 + 1; sum = sum2DArray(arr2, ptr_arr_start, ptr_arr_end); // int a = 100, b = 200; // printf("before swap a is %d, b is %d\n", a, b); // swap(&a, &b); // printf("after swap a is %d, b is %d\n", a, b); // int p_height; // int p_age; // int *p_arr = createArray(MAX); // int i; // fun(&p_height, &p_age); // printf("height is %d and age is %d\n", p_height, p_age); // for (i = 0; i < MAX; i++) // { // printf("%d\n", p_arr[i]); // } // int c = 30; // int *ptr1 = &c; // int **ptr2 = &ptr1; // printf("c is %d\n", c); // printf("ptr1 is %d\n", *ptr1); // printf("ptr2 is %d\n", **ptr2); }
标签:arr,语言,int,add,swap,printf,ptr From: https://www.cnblogs.com/dogingate/p/17858581.html