例65:宏定义的使用
#include <stdio.h> // 宏定义 - 程序中所有ARRAY_SIZE在编译时替换为数字5 #define ARRAY_SIZE 5 int main(int argc, char const *argv[]) { // int values[5] = {80, 70, 90, 85, 80} int values[ARRAY_SIZE] = {80, 70, 90, 85, 80}; int i; for (i = 0; i < ARRAY_SIZE; i++) printf("values[%d] %d\n", i, values[i]); return 0; }例66:数组初始化和访问
#include <stdio.h> int main(int argc, char const *argv[]) { int values[5] = {80, 70, 90, 85, 80}; int i; for (i = 0; i < 5; i++) printf("values[%d] %d\n", i, values[i]); return 0; }例67:打印变量地址值
#include <stdio.h> int main(int argc, char const *argv[]) { int count = 1; float salary = 40000.0; long distance = 1234567L; // %x表示用十六进制打印 printf("Address of count is %x\n", &count); // Address of count is 61ff1c printf("Address of salary is %x\n", &salary); // Address of salary is 61ff18 printf("Address of distance is %x\n", &distance); // Address of distance is 61ff14 return 0; }例68:打印数组地址值
#include <stdio.h> int main(int argc, char const *argv[]) { int count[10]; float salaries[5]; long distances[10]; // 数组名就是数组的首地址 printf("Address of the array count is %x\n", count); //Address of the array count is 61fef8 printf("Address of the array salaries is %x\n", salaries); //Address of the array salaries is 61fee4 printf("Address of the array distances is %x\n", distances);//Address of the array distances is 61febcs return 0; }例69:打印数组所占字节数
#include <stdio.h> int main(int argc, char const *argv[]) { // 100个int型元素 int scores[100]; // 100个float型元素 float salaries[100]; // 100个char型元素 char string[100]; printf("Bytes used to hold int scores[100] is %d bytes\n", sizeof(scores)); // Bytes used to hold int scores[100] is 400 bytes printf("Bytes used to hold int salaries[100] is %d bytes\n", sizeof(salaries)); // Bytes used to hold int salaries[100] is 400 bytes printf("Bytes used to hold char string[100] is %d bytes\n", sizeof(string)); // Bytes used to hold char string[100] is 100 bytes return 0; }例70:两种方式表示数组的首地址
#include <stdio.h> int main(int argc, char const *argv[]) { int count[10]; float salaries[5]; long distances[10]; printf("Address of the array count is %x &count is %x\n", count, &count); // Address of the array count is 61fef8 &count is 61fef8 printf("Address of the array salaries is %x &count is %x\n", salaries, &salaries); // Address of the array salaries is 61fee4 &count is 61fee4 printf("Address of the array distances is %x &distances is %x\n", distances, &distances); //Address of the array distances is 61febc &distances is 61febc return 0; }例71:使用函数打印数组元素
#include <stdio.h> /** * 在控制台打印数组中的所有元素 * @Author dust_fall * @param values 要打印的数组 * @param number_of_elements 数组元素的个数 */ void show_array(int values[], int number_of_elements) { int i; printf("About to display %d values\n", number_of_elements); for (i = 0; i < number_of_elements; i++) printf("%d\n", values[i]); } int main(int argc, char const *argv[]) { int scores[5] = {70, 80, 90, 100, 90}; int count[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int small[2] = {-33, -44}; show_array(scores, 5); show_array(count, 10); show_array(small, 2); return 0; }例72:使用二分查找法查找数组元素
#include <stdio.h> /** * 二分查找数组元素(要求数组有序) * 将要查找的元素和有序数组的中间元素比较, 大了就在前一半数组找, 小了就在后一半数组找 * 比较方法同上, 想不明白的可以思考一下“数字炸弹”的小游戏, 不过这里每次对半而已 * @Author dust_fall * @param array 数组 * @param value 要找的元素 * @param size 数组元素个数 * @return 查找的元素 */ int binary_search(int array[], int value, int size) { // 标志位 - 为1表示找到 int found = 0; int high = size, low = 0, mid; // 正中元素的下标 mid = (high + low) / 2; printf("\n\nLooking for %d\n", value); while ((!found) && (high >= low)) { printf("Low %d Mid %d High %d\n", low, mid, high); if (value == array[mid]) found = 1; else if (value < array[mid]) high = mid - 1; else low = mid + 1; // 更新正中元素的下标 mid = (high + low) / 2; } return((found) ? mid: -1); } int main(int argc, char const *argv[]) { int array[100], i; for (i = 0; i < 100; i++) array[i] = i; printf("Result of search %d\n", binary_search(array, 33, 100)); printf("Result of search %d\n", binary_search(array, 75, 100)); printf("Result of search %d\n", binary_search(array, 1, 100)); printf("Result of search %d\n", binary_search(array, 1001, 100)); return 0; }例73:使用标准库中二分查找函数bsearch
#include <stdlib.h> #include <stdio.h> /** * 标准库中的二分查找函数 * 函数原型void *bsearch(const void *key, const void *base, size_t num, size_t size, int (*cmp)(const void *, const void *)); * key 指向要查找的元素 * base 指向进行查找的数组 * num 数组中元素的个数 * size 数组中每个元素的大小,一般用sizeof()表示 * cmp 比较两个元素的函数,定义比较规则。需要注意的是,查找数组必须是经过预先排序的,而排序的规则要和比较子函数cmp的规则相同。 * 如果找到元素则返回指向该元素的指针,否则返回NULL */ // 以下这两种方式是一样 - 两个参数相等时返回0 int compare_int(int *a, int *b) { return(*a - *b); } int compare_float(float *a, float *b) { return((*a == *b) ? 0: 1); } int main(int argc, char const *argv[]) { int int_values[] = {1, 3, 2, 4, 5}; float float_values[] = {1.1, 3.3, 2.2, 4.4, 5.5}; int elements = 5; int *int_ptr, int_value = 2; float *float_ptr, float_value = 33.3; // 这里按照开头的函数原型写, 改强转就强转 int_ptr = (int*)bsearch(&int_value, int_values, elements, sizeof(int), (int (*) (const void *, const void *)) compare_int); if (*int_ptr) printf("Value %d found\n", int_value); else printf("Value %d not found\n", int_value); float_ptr = (float*)bsearch(&float_value, float_values, elements, sizeof(float), (int (*) (const void *, const void *)) compare_float); if (*float_ptr) printf("Value %3.1f found\n", float_value); else printf("Value %3.1f not found\n", float_value); return 0; }例74:用函数排序数组
#include <stdio.h> #include <stdlib.h> /** * 将数组从小到大排序 * @Author dust_fall * @param array 要排序的数组 * @param size 数组的大小 */ void bubble_sort(int array[], int size) { int temp, i, j; // 外层循环遍历数组下标 for (i = 0; i < size; i++) // 内层循环遍历数组下标 for (j = 0; j < size; j++) // 将外层下标元素与所有元素比较, 找到第i+1小的元素放到第i个位置 if (array[i] < array[j]) { // 交换 temp = array[i]; array[i] = array[j]; array[j] = temp; } // 思考:如何优化比较循环次数? } int main(int argc, char const *argv[]) { int values[30], i; // 用随机数给数组赋值 for (i = 0; i < 30; i++) values[i] = rand() % 100; bubble_sort(values, 30); for (i = 0; i < 30; i++) printf("%d ", values[i]); return 0; }