《C和指针》学习笔记[第八章 数组]
8.7问题
4.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #define SIZE 100 int main(void){ char buffer[SIZE] = "12321"; int front, rear; front = 0; rear = strlen(buffer) - 1; while (front < rear) { if (buffer[front] != buffer[rear]) { break; } front++; rear--; } if (front >= rear) { printf("It is a palindrome!\n"); } return EXIT_SUCCESS; }
指针操作
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #define SIZE 100 int main(void){ char buffer[SIZE] = "125521"; char *front, *rear; front = buffer; rear = front + strlen(buffer) - 1; while (front < rear) { if (*front != *rear) { break; } front++; rear--; } if (front >= rear) { printf("It is a palindrome!\n"); } return EXIT_SUCCESS; }
5.
下表看过去更加舒服,代码容易理解。
6.
汇编全忘了,略
7.
跳过
8.
经过本人测试,可以证实*a 与a[10]属于不同的对象,该题主要的作用为告知数组与指针的不同。
标准答案,抄袭书本。
在第1个赋值中,编译器认为a是一个指针变量,所以它提取存储在那里的指针值,并加上12(3和整数的长度相乘),然后对这个结果执行间接访问操作。但a实际上是整型数组的起始位置,所以作为"指针"获得的这个值实际上是数组的第一个整型元素。它与12相加,其结果解释为一个地址,然后对它进行间接访问。作为结果,它或者将提取一些任意内存位置的内容,或者由于某种地址错误而导致程序失败。
在第2个赋值中,编译器认为b是一个数组名,所以它把12(3的调整结果)加到b的存储地址,然后间接访问操作从那里活得值。事实上,b是个指针变量,所以从内存中提取的后面三个子实际上是从另外的任意变量中获得的。这个问题说明了指针和数组虽然存在关联,但绝不是相同的。
9.
int main(void){ int coin_values[] = {1, 2, 5, 10}; return EXIT_SUCCESS; }
12.
当执行任何"按照元素在内存中出现的顺序对元素进行访问"的操作时。列如,初始化一个数组、读取或者写入超过一个的数组元素、通过移动指针访问数组的底层内存"压扁"数组等都属于这类操作。
8.8编程练习
1.
略,确实太占字符与长度了。
2.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #include "t_c.h" #define RANGE 5 float in_come_array[RANGE] = {0, 23350, 56550, 117950, 256500}; float base_tax[RANGE] = {0 , 3502.5, 12798.5, 31832.5, 81710.5}; float tax_rate[RANGE] = {.15, .28, .31, .36, .396}; float single_tax(float income){ float tax = -1; for (int i = RANGE - 1; i >= 0; i--) { if (income > in_come_array[i]) { tax = base_tax[i] + (income - in_come_array[i]) * tax_rate[i]; break; } } return tax; } int main(void){ float income = 1000000; float tax; tax = single_tax(income); printf("icncome = %.2f, tax = %.2f\n", income, tax); return EXIT_SUCCESS; }
3.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #include "t_c.h" #define RANGE 10 int identity_matrix(int (*p)[RANGE]){ for (int i = 0; i < RANGE; i++) { for (int j = 0; j <= i; j++) { if (i == j) { if (p[i][j] != 1) { return 0; } } else{ if (p[i][j] != 0) { return 0; } } } } return 1; } void init_matrix(int matrix[RANGE][RANGE]){ for (int i = 0; i < RANGE; i++) { for (int j = 0; j <= i; j++) { if (i == j) { matrix[i][j] = 1; } else matrix[i][j] = 0; } } } int main(void){ int matrix[10][10]; int res; init_matrix(matrix); // matrix[3][2] = 1; res = identity_matrix(matrix); printf("is ok %d\n", res); return EXIT_SUCCESS; }
4.
上一道题目答案有误,在本题的答案中进行更正
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #include "t_c.h" #define RANGE 5 int identity_matrix(int *i_p, int dimension){ int (*p)[dimension] = (int (*)[dimension]) i_p; for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { // printf("i = %d, j = %d, %d\n",i, j, p[i][j]); if (i == j) { if (p[i][j] != 1) { return 0; } } else{ if (p[i][j] != 0) { return 0; } } } } return 1; } void init_matrix(int matrix[RANGE][RANGE]){ for (int i = 0; i < RANGE; i++) { for (int j = 0; j < RANGE; j++) { if (i == j) { matrix[i][j] = 1; } else matrix[i][j] = 0; } } } void show_matrix(int matrix[RANGE][RANGE]){ for (int i = 0; i < RANGE; i++) { for (int j = 0; j < RANGE; j++) { printf("%d ", matrix[i][j]); } putchar('\n'); } } int main(void){ int matrix[RANGE][RANGE]; int res; init_matrix(matrix); // show_matrix(matrix); res = identity_matrix(&matrix[0][0], RANGE); printf("is ok %d\n", res); return EXIT_SUCCESS; }
5.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #include "t_c.h" void matrix_multiply(int *m1, int *m2, int *r, int x, int y, int z){ for (int i = 0; i < x; i++) { for (int j = 0; j < z ; j++) { for (int k = 0; k < y; k++) { r[i * z + j] += m1[i * y + k] * m2[j + k * z]; } } } } void show_matrix(int *matrix, int x, int y){ for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { printf("%d ", matrix[i * y + j]); } putchar('\n'); } } int main(void){ int m1[3][2] = {{2, -6}, {3, 5}, {1 , -1}}; int m2[2][4] = {{4, -2, -4, -5},{-7, -3, 6, 7}}; int r[3][4] = {0}; int *p1 = &m1[0][0]; int *p2 = &m2[0][0]; int *p3 = &r[0][0]; matrix_multiply(p1, p2, p3, 3, 2, 4); show_matrix(p3, 3, 4); return EXIT_SUCCESS; }
6.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #include "t_c.h" int array_offset(int arrayinfo[], ...){ int dimension = arrayinfo[0]; int offset = 1; int multiple = 0; int this_arg; int this_hight, this_low; va_list var_arg; // 检测维度不能超过指定范围 if (dimension > 10 || dimension < 1) { return -1; } va_start(var_arg, arrayinfo); for (int i = 0; i < dimension; i++) { this_arg = va_arg(var_arg, int); this_low = arrayinfo[i * 2 + 1]; this_hight = arrayinfo[i * 2 + 2]; if (this_arg < this_low || this_arg > this_hight) { return -1; } if (i == 0 && dimension > 1) { multiple = this_arg - this_low; // printf("multiple = %d\n", multiple); } else { if (i == dimension - 1) { offset = multiple * (this_hight - this_low + 1) + this_arg - this_low; } else { multiple = multiple * (this_hight - this_low + 1) + this_arg - this_low; // printf("this_arg = %d\n", this_arg); // printf("multiple = %d\n", multiple); } } } va_end(var_arg); return offset; } int main(void){ int arrayinfo[] = {3, 4, 6 ,1, 5, -3, 3}; int offset; offset = array_offset(arrayinfo, 4, 2, -3); printf("offset = %d \n", offset); return EXIT_SUCCESS; }
7.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #include "t_c.h" int array_offset2(int arrayinfo[], ...){ int dimension = arrayinfo[0]; int offset = 1; int multiple = 0; int base_step = 0; int first_offset = 0; int base_num = 0; int this_arg; int this_hight, this_low; va_list var_arg; // 检测维度不能超过指定范围 if (dimension > 10 || dimension < 1) { return -1; } va_start(var_arg, arrayinfo); for (int i = 0; i < dimension; i++) { this_arg = va_arg(var_arg, int); this_low = arrayinfo[i * 2 + 1]; this_hight = arrayinfo[i * 2 + 2]; if (this_arg < this_low || this_arg > this_hight) { return -1; } if (i == 0) { base_step = this_hight - this_low + 1; first_offset = this_arg - this_low; } else { if (i == 1) { multiple = this_arg - this_low; base_num = this_hight - this_low + 1; } else { multiple =(this_arg - this_low) * base_num + multiple; base_num *= (this_hight - this_low + 1); } if (i == dimension - 1) { offset = multiple * base_step + first_offset; } // printf("multiple = %d, base_step = %d, first_offset = %d\n", multiple, base_step, first_offset); } } va_end(var_arg); return offset; } int main(void){ int arrayinfo[] = {3, 4, 6 ,1, 5, -3, 3}; int offset; offset = array_offset2(arrayinfo, 4, 1, -1); printf("offset = %d \n", offset); return EXIT_SUCCESS; }
8.
// // t_c.c // pointers_on_c // // Created by sidian on 2022/5/27. // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <stdarg.h> #include "t_c.h" #define MATRIX_SIZE 8 int count_num = 0; void show_matrix(int matrix[][MATRIX_SIZE]); // 获取可以用的column的数值,也就是第几列 int get_used_column_num(int matrix[][MATRIX_SIZE], int in_row, int start_column){ int column = start_column; int row; int pre_row; int pre_column_l; int pre_column_r; int is_pass; if (in_row >= MATRIX_SIZE) { return -1; } // printf("row = %d start_column = %d\n", row, start_column); for (; column < MATRIX_SIZE; column++) { is_pass = 1; row = in_row; pre_column_l = column; pre_column_r = column; while ((pre_row = row - 1) >= 0) { pre_column_l--; pre_column_r++; row--; if (pre_column_l >= 0 && matrix[pre_row][pre_column_l] == 1) { is_pass = 0; break; } if (pre_column_r < MATRIX_SIZE && matrix[pre_row][pre_column_r] == 1) { is_pass = 0; break; } if (matrix[pre_row][column] == 1) { is_pass = 0; break; } } // printf("in_row = %d column = %d is_pass = %d\n", in_row, column, is_pass); if (is_pass) { return column; } } return -1; } int running(int matrix[][MATRIX_SIZE], int row, int start_column){ int res; int this_column_num; // 计算可用的列 this_column_num = get_used_column_num(matrix, row, start_column); // printf("row = %d, start_column = %d, this_column_num = %d\n",row,start_column, this_column_num); // 无可用的列返回给上层的函数 if (this_column_num == -1) { return -1; } // 有可用的列,进入递归 else{ matrix[row][this_column_num] = 1; if (row == MATRIX_SIZE - 1) { show_matrix(matrix); count_num++; // return -1; } res = running(matrix, row + 1, 0); while (res == -1) { matrix[row][this_column_num] = 0; this_column_num = get_used_column_num(matrix, row, this_column_num + 1); if (this_column_num != -1) { matrix[row][this_column_num] = 1; res = running(matrix, row + 1, 0); }else{ break; } } return -1; } } void show_matrix(int matrix[][MATRIX_SIZE]){ for (int row = 0; row < MATRIX_SIZE; row++) { for (int column = 0; column < MATRIX_SIZE; column++) { printf("%d ", matrix[row][column]); } putchar('\n'); } putchar('\n'); } void init_matrix(int matrix[][MATRIX_SIZE]){ for (int row = 0; row < MATRIX_SIZE; row++) { for (int column = 0; column < MATRIX_SIZE; column++) { matrix[row][column] = 0; } } } int main(void){ int res; int matrix[MATRIX_SIZE][MATRIX_SIZE]; // show_matrix(matrix); for (int i = 0; i < MATRIX_SIZE; i++) { init_matrix(matrix); res = running(matrix, 0, i); } printf("count_num = %d\n",count_num); return EXIT_SUCCESS; }