C语言学习5:数组定义,数组名的含义,数组作为函数参数,数组赋值,数组左右移动,最大最小,反向遍历,冒泡排序,sizeof 和strlen区别,gets和puts,strcpy,strcat,strcmp,strchr,strstr,strlen
1,数组定义
#include <stdio.h> int main(void) { // 定义数组时需要确定: // 1. 数组元素的类型. // 2. 显式/隐式确定数组元素个数. int a[6] = { 12, 25, 36, 8, 45, 66 }; // 隐式确定数组元素个数 int b[] = { 3, 4, 5, 18, 23, 99}; // C99 // 在具有初始化式时, 数组中剩下的元素被初始化为 0 int c[] = { [0] = 36, [4] = 82 }; int i; for (i = 0; i < 6; i++) printf("a[%d] = %d\n", i, a[i]); printf("----------------------\n"); for (i = 0; i < 6; i++) printf("b[%d] = %d\n", i, b[i]); printf("----------------------\n"); for (i = 0; i < 5; i++) printf("c[%d] = %d\n", i, c[i]); return 0; }
结果: 证明在数组里面[0],[4]可以用来定义某个元素
a[0] = 12 a[1] = 25 a[2] = 36 a[3] = 8 a[4] = 45 a[5] = 66 ---------------------- b[0] = 3 b[1] = 4 b[2] = 5 b[3] = 18 b[4] = 23 b[5] = 99 ---------------------- c[0] = 36 c[1] = 0 c[2] = 0 c[3] = 0 c[4] = 82
2,数组名
#include <stdio.h> int main(void) { int a[5]; printf("sizeof a = %d\n", sizeof a); printf("sizeof a[0] = %d\n", sizeof a[0]); printf("a has %d elements.\n", sizeof a / sizeof a[0]); printf("===============\n"); // 数组名代表数组首元素地址. printf("a = %p\n", a); printf("&a[0] = %p\n", &a[0]); return 0; }
结果:可以看出数组名就是首地址
will@will-Inspiron-N4010:~/c/4th$ ./a.out sizeof a = 20 sizeof a[0] = 4 a has 5 elements. =============== a = 0xbf847f6c &a[0] = 0xbf847f6c
3,数组作为函数参数
#include <stdio.h> // 当数组作为函数参数的时候, // 实际上退化为一个指针. // int a[10] --> int *a void foo(int a[10]) { printf("in foo, sizeof a = %d\n", sizeof a); } int main(void) { int a[10]; printf("in main, sizeof a = %d\n", sizeof a); foo(a); return 0; }
结果:可以看出结果,在函数里数组名只是一个指针而已
will@will-Inspiron-N4010:~/c/4th$ ./a.out in main, sizeof a = 40 in foo, sizeof a = 4
4,数组赋值
#include <stdio.h> void rand_a(int a[], int len) { int i; for (i = 0; i < len; i++) a[i] = rand() % 100; } void print_a(int a[], int len) { int i; for (i = 0; i < len; i++) printf("%d ", a[i]); putchar('\n'); } int main(void) { int a[10]; rand_a(a, 10); print_a(a, 10); return 0; }
结果:
will@will-Inspiron-N4010:~/c/4th$ ./a.out 83 86 77 15 93 35 86 92 49 21
5,数组左移右移,最大最小,反向遍历,冒泡排序
#include <stdio.h> void rand_a(int a[], int len) { int i; for (i = 0; i < len; i++) a[i] = rand() % 100; } void print_a(int a[], int len) { int i; for (i = 0; i < len; i++) printf("%d ", a[i]); putchar('\n'); } int max(int a[], int len) { int i, max = a[0]; for (i = 0; i < len; i++) if (a[i] > max) max = a[i]; return max; } int min(int a[], int len) { int i, min = a[0]; for (i = 0; i < len; i++) if (a[i] < min) min = a[i]; return min; } void __move_left(int a[], int len) { int i, t = a[0]; for (i = 0; i < len - 1; i++) a[i] = a[i + 1]; a[i] = t; } void move_left(int a[], int len, int shift) { int i; shift %= len; for (i = 0; i < shift; i++) __move_left(a, len); } void __move_right(int a[], int len) { int i; for (i = len - 1; i > 0; i--) { a[i] ^= a[i - 1]; a[i - 1] ^= a[i]; a[i] ^= a[i - 1]; } } void move_right(int a[], int len, int shift) { int i; shift %= len; for (i = 0; i < shift; i++) __move_right(a, len); } void reverse(int a[], int len) { int i; for (i = 0; i < len / 2; i++) { a[i] ^= a[len - i - 1]; a[len - i - 1] ^= a[i]; a[i] ^= a[len - i - 1]; } } void bubble_sort(int a[], int len) { int i, j; for (i = 0; i < len - 1; i++) { for (j = 0; j < len - i - 1; j++) { if (a[j] > a[j + 1]) { a[j] ^= a[j + 1]; a[j + 1] ^= a[j]; a[j] ^= a[j + 1]; } } } } int main(void) { int a[10]; rand_a(a, 10); print_a(a, 10); printf("max: %d\n", max(a, 10)); printf("min: %d\n", min(a, 10)); printf("move left 3:\n"); move_left(a, 10, 3); print_a(a, 10); printf("move right 3:\n"); move_right(a, 10, 3); print_a(a, 10); printf("reverse:\n"); reverse(a, 10); print_a(a, 10); printf("sort:\n"); bubble_sort(a, 10); print_a(a, 10); return 0; }
结果:这个些操作里应掌握
will@will-Inspiron-N4010:~/c/4th$ ./a.out 83 86 77 15 93 35 86 92 49 21 max: 93 min: 15 move left 3: 15 93 35 86 92 49 21 83 86 77 move right 3: 83 86 77 15 93 35 86 92 49 21 reverse: 21 49 92 86 35 93 15 77 86 83 sort: 15 21 35 49 77 83 86 86 92 93
6,sizeof和strlen区别
#include <stdio.h> #include <string.h> int main(void) { // char s[10] = { 'c', 'h', 'i', 'n', 'a', '\0' }; char s[10] = "china"; // sizeof 是运算符, 在编译期求出结果. printf("sizeof \"china\" = %d\n", sizeof "china"); printf("sizeof s = %d\n", sizeof s); // strlen 是库函数, 在运行期求出结果. printf("strlen of s = %d\n", strlen(s)); return 0; }
结果:可以看出sizeof能完整得到大小,strlen 是用来得到字符个数的,
will@will-Inspiron-N4010:~/c/4th$ ./a.out sizeof "china" = 6 sizeof s = 10 strlen of s = 5
7,gets和puts
#include <stdio.h> int main(void) { char s[64]; printf("input a string: "); gets(s); puts("you inputed: "); puts(s); // printf("%s", s); return 0; }
结果:仅仅是输入输出函数而已
input a string: 23423423 you inputed: 23423423
8,strcpy函数
#include <stdio.h> #include <string.h> int main(void) { char src[128]; char dst[128]; printf("input a string: "); gets(src); strcpy(dst, src); printf("dst = %s\n", dst); return 0; }
结果:字符串复制
will@will-Inspiron-N4010:~/c/4th$ ./a.out input a string: what your name dst = what your name
9,strcat续接函数
#include <stdio.h> #include <string.h> int main(void) { char s1[128] = "china unix "; char s2[64]; printf("input s2: "); gets(s2); strcat(s1, s2); printf("s1 = %s\n", s1); return 0; }
结果:续接函数。
will@will-Inspiron-N4010:~/c/4th$ ./a.out input s2: xxxx xxx s1 = china unix xxxx xxx
10,strcmp 比较函数
#include <stdio.h> #include <string.h> int main(void) { char s1[128]; char s2[128]; printf("input s1: "); gets(s1); printf("input s2: "); gets(s2); if (strcmp(s1, s2) > 0) printf("%s > %s\n", s1, s2); else if (strcmp(s1, s2) < 0) printf("%s < %s\n", s1, s2); else printf("%s == %s\n", s1, s2); return 0; }
结果:
will@will-Inspiron-N4010:~/c/4th$ ./a.out input s1: what input s2: njat what > njat will@will-Inspiron-N4010:~/c/4th$ ./a.out input s1: what input s2: what what == what
11,strchr字符比较函数
#include <stdio.h> #include <string.h> int main(void) { int ch = 0; char s[128]; puts("input s: "); gets(s); puts("input ch: "); ch = getchar(); if (strchr(s, ch)) printf("%s has character %c\n", s, ch); else printf("%s hasn't character %c\n", s, ch); return 0; }
结果:函数的返回值是指向字符所在的指针
will@will-Inspiron-N4010:~/c/4th$ ./a.out input s: yournameis input ch: w yournameis hasn't character w will@will-Inspiron-N4010:~/c/4th$ ./a.out input s: yournameis input ch: a yournameis has character a
12,strstr字符串比较函数
#include <stdio.h> #include <string.h> int main(void) { char s[1024]; char substr[64]; printf("input s: "); gets(s); printf("input substr: "); gets(substr); if (strstr(s, substr)) printf("%s has %s\n", s, substr); else printf("%s hasn't %s\n", s, substr); return 0; }
结果:
will@will-Inspiron-N4010:~/c/4th$ ./a.out input s: whtasdfis input substr: is whtasdfis has is