1.字符串定义
1 2 3 4 5
| char *str = "JACKOU";
char string[] = {'J', 'A', 'C', 'K', 'O', 'U','\0'};
|
1 2 3 4 5 6 7 8 9 10 11
| int mainT2() { char str[] = {'D', 'e', 'r', 'r', 'y', '\0'}; str[2] = 'z'; printf("第一种方式:%s\n", str);
char * str2 = "Derry"; str2[2] = 'z'; printf("第二种方式:%s\n", str);
return 0; }
|
2.计算字符串长度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| int getLen(char *string) { int count = 0; while (*string) { string++; count++; } return count; }
int intArr[] = {1, 2, 3, 4, 5, 6, 7, 8, '\0'}; int len = sizeof(intArr) / sizeof(int);
void getLen(int intArr[]) { int len = sizeof(intArr) / sizeof(int); }
int len = (int) strlen(string);
|
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
| void getLen(int * resultLen, int intarr[]) {
int count = 0; while (*intarr != '\0') { intarr++; count++; } *resultLen = count; }
int mainT3() {
int intarr[] = {1,2,3, 0,5,6,7};
int len = sizeof(intarr) / sizeof(int); printf("len长度是:%d\n", len);
int result; getLen(&result, intarr); printf("getLen len长度是:%d\n", result);
return 0; }
|
3.字符串转换
1 2 3 4 5 6 7 8 9 10 11
| atoi(num);
atof();
atol();
atoll();
|
4.字符串比较
1 2 3 4 5 6
| char *str1 = "jackou1"; char *str2 = "jackou";
result = strcmp(str1, str2);
|
5. 字符串查找
1 2 3 4 5 6 7 8
| char *str1 = "jackou1"; char *str2 = "k"; int *p = strstr(str1,str2);
int index = p - str1;
|
1 2 3 4 5
| void subString(char *result, char *str, int start, int end) { for (int i = start; i < end; i++) { *(result++) = *(str + i); } }
|
6.结构体
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
| #include <stdio.h>
struct Dog { char name[10]; int age; char sex; };
struct Study { char *content; }; struct Student { char name[10]; int age; char sex;
struct Study study;
struct Wan { char *content; } wan; };
int main() { struct Dog dog; printf("dog: name=%s, age=%d, sex=%d \n", dog.name, dog.age, dog.sex); return 0; }
|
7.结构体指针
1 2 3 4 5 6 7 8 9 10 11 12
| struct Cat { char *name; int age; };
int main() { struct Cat cat = {"cat", 5}; struct Cat *catP = &cat; printf("name=%s,age=%d \n", catP->name, catP->age);
return 0; }
|
8.正确的方法结果接收写法
1 2 3 4 5 6 7 8 9 10
| int main(){ char *result = "abc"; char *result1 = NULL;
char *result2 = ""; action(result2);
char result3[100] = "abc"; action(result3); }
|
9. typedef
为了兼容方法内部的内容不做改变,使用typedef来定义结构体
1 2 3 4 5 6 7 8 9 10
| typedef struct Dog{ char name[10]; int age; } Dog;
typedef struct { char name[10]; int age; } Person;
|
10.字符串拼接
1 2 3 4 5 6 7
| char dest[25]; char* blank = "--到--"; char* cpp = "cpp"; char* java = "java"; strcpy(dest, cpp); strcat(dest,blank); strcat(dest,blank);
|
11.字符串转化
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
| #include <stdio.h> #include <ctype.h>
void lower(char * dest, char * name) { char * temp = name; while (*temp) { *dest = tolower(*temp); temp ++; dest ++; } *dest = '\0';
printf("不能破坏 name:%s\n", name); }
int mainT6() { char * name = "DerrY";
char dest[20]; lower(dest, name); printf("小写转换后的结构是:%s\n", dest);
return 0; }
|
12.结构体定义与使用
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
| #include <stdio.h> #include <string.h> #include <stdlib.h> #include <malloc.h>
struct Dog { char name[10]; int age; char sex;
};
int main() { struct Dog dog; printf("name:%s, age:%d, sex:%c \n", dog.name, dog.age, dog.sex);
strcpy(dog.name, "旺财"); dog.age = 3; dog.sex = 'G'; printf("name:%s, age:%d, sex:%c \n", dog.name, dog.age, dog.sex); return 0; }
|
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
| struct Person { char * name; int age; char sex; } ppp = {"Derry", 33, 'M'}, ppp2, ppp3, pppp4, pppp5
; int main() {
printf("name:%s, age:%d, sex:%c \n", ppp.name, ppp.age, ppp.sex);
pppp5.name = "DerryO"; pppp5.age = 4; pppp5.sex = 'M'; printf("name:%s, age:%d, sex:%c \n", pppp5.name, pppp5.age, pppp5.sex); return 0; }
|
13.结构体指针与动态开辟空间
如果是结构体指针,使用->
来引用,如果是结构体,使用.
来引用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <stdio.h> #include <string.h>
struct Cat { char name[10]; int age; };
int main() {
struct Cat cat = {"小花猫", 2};
struct Cat * catp = &cat; catp->age = 3; strcpy(catp->name, "小花猫2"); printf("name:%s, age:%d \n", catp->name, catp->age);
return 0; }
|
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
| #include <stdio.h> #include <string.h> #include <stdlib.h>
struct Cat2 { char name[10]; int age; };
int main() {
struct Cat2 *cat = malloc(sizeof(struct Cat2));
strcpy(cat->name, "金色猫"); cat->age = 5;
printf("name:%s, age:%d \n", cat->name, cat->age);
free(cat); cat = NULL;
return 0; }
|
14.结构体数组
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 46 47 48 49 50 51 52 53
| #include <stdio.h> #include <stdlib.h> #include <string.h>
struct Cat3 { char name[10]; int age; };
int main() {
struct Cat3 cat [10] = { {"小黄", 1}, {"小白", 2}, {"小黑", 3}, {}, {}, {}, {}, {}, {}, {}, };
struct Cat3 cat9 = {"小黑9", 9}; *(cat + 9) = cat9; printf("name:%s, age:%d \n", cat9.name, cat9.age);
struct Cat3 * cat2 = malloc(sizeof(struct Cat3) * 10);
strcpy(cat2->name, "小花猫000"); cat2->age = 9; printf("name:%s, age:%d \n", cat2->name, cat2->age);
cat2 += 7; strcpy(cat2->name, "小花猫888"); cat2->age = 88; printf("name:%s, age:%d \n", cat2->name, cat2->age);
free(cat2); cat2 = NULL;
return 0; }
|
15.结构体与结构体指针取别名
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
| #include <stdio.h> #include <stdlib.h>
struct Workder_ { char name[10]; int age; char sex; };
typedef struct Workder_ Workder_;
typedef Workder_ * Workder;
int main() {
Workder_ workder1 = malloc(sizeof(Workder_));
Workder workder = malloc(sizeof(Workder_));
return 0; }
|
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 46 47 48 49 50 51 52
| #include <stdio.h> #include <stdlib.h>
struct DAO { char name[10]; int age; char sex; };
typedef struct { char name[10]; int age; char sex; };
typedef struct { char name[10]; int age; char sex; } AV;
typedef struct DAO DAO;
void show(DAO dao) {}
int main() {
DAO * dao = malloc( sizeof(DAO));
DAO * dao1 = malloc( sizeof(DAO));
DAO * dao2 = malloc( sizeof(DAO));
AV av = {"VideoInfo", 54, 'M'};
AV * avp = malloc(sizeof(AV));
return 0; }
|
16.枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <stdio.h>
enum CommentType { TEXT = 10, TEXT_IMAGE, IMAGE };
typedef enum CommentType CommentType;
int main() { enum CommentType commentType = TEXT; enum CommentType commentType1 = TEXT_IMAGE; enum CommentType commentType2 = IMAGE;
printf("%d, %d, %d \n", commentType, commentType1, commentType2);
return 0; }
|
版权声明: 此文章版权归Jack Ou所有,如有转载,请註明来自原作者