---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
1. 不用strcat连接字符串,示例代码:
/* 编写一个函数void strlink(char s[], char t[]) 将字符串t连接到字符串s的尾部 */ #include <stdio.h> void strlink(char s[], char t[]); int main() { char s1[20] = "michael "; char s2[] = "jackson"; strlink(s1, s2); printf("%s\n", s1); return 0; } void strlink(char s[], char t[]) { int i = 0; // 判断s[i]是否为字符串的尾部 while ( s[i] != '\0' ) { i++; } int j = 0; // 拷贝t的内容到s的后面 while ( (s[i] = t[j]) != '\0' ) { i++; j++; } } /* 更加精简的写法,仅作为参考(会有警告信息) void strlink2(char s[], char t[]) { int i = -1; // 判断s[i]是否为字符串的尾部 while (s[++i]) ; int j = 0; // 拷贝t的内容到s的后面 while (s[i++] = t[j++]) ; }*/
2. 不用strcat连接字符串(函数参数为指针变量),示例代码:
/* 编写一个函数void strlink(char *s, char *t) 将字符串t连接到字符串s的尾部 */ #include <stdio.h> void strlink(char *s, char *t); int main() { char s1[20] = "michael "; char s2[] = "jackson"; strlink2(s1, s2); printf("%s\n", s1); return 0; } void strlink(char *s, char *t) { // 判断s[i]是否为字符串的尾部 while ( *s != '\0' ) { s++; } // 拷贝t的内容到s的后面 while ( (*s = *t) != '\0' ) { s++; t++; } } /* 更加精简的写法,仅作为参考(会有警告信息) void strlink2(char *s, char *t) { // 判断s[i]是否为字符串的尾部 while (*s++); // 减回一次 s--; // 拷贝t的内容到s的后面 while ( *s++ = *t++ ) ; }*/
3. 字符串指针,示例代码:
/* 编写一个函数,判断某个字符串是否为回文。 回文就是从左边开始读 和 从右边开始读 都是一样的,比如"abcba" */ #include <string.h> #include <stdio.h> int isHuiwen(char *str); int main() { printf("%d\n", isHuiwen("a")); return 0; } /* 返回1代表是回文 返回0代表不是回文 */ int isHuiwen(char *str) { // 1.定义一个指向变量left指向字符串的首字符 char *left = str; // 2.定义一个指向变量right指向字符串的末字符 char *right = str + strlen(str) - 1; while (left < right) { // 如果左边和右边的字符不一样 if (*left++ != *right--) { return 0; } } return 1; }
4. struct结构体定义变量名易犯错误:
#include <stdio.h> #define String char * int main() { String s1, s2; s1 = "jack"; // s2并不是char *类型,而是char类型 s2 = 'A'; // 只能赋值一个字符 //s2 = "rose"; return 0; }
这里面的String s1, s2;翻译过来就是Char * s1, s2;所以s2只是char类型的,并不是char *类型(char*型指针变量)如果写成:
String s1; String s2; // 这样写就可以了
5. 指向函数的指针易犯错误处:
#include <stdio.h> int sum(int a, int b) { printf("哈哈哈\n"); return a+b; } int main() { int (*p)(int, int); p = sum; // p指向sum函数 int c = (*p)(10, 11); // 利用指针实现函数的掉用和int c = sum((10, 11));等价 printf("和是%d\n", c); return 0; }
6. #ifndef和#endif的配合使用(切记!)
#include <stdio.h> #define PI 3.14 int main() { #ifndef PI printf("没有定义PI这个宏\n"); #else printf("定义了PI这个宏\n"); return 0; #endif }
7. scanf输入普通变量时,要求通知计算机输入变量所在的地址是多少,所有加&符号表示相应变量的地址;而输入字符串时,数组名恰好是字符串的首地址,或者说数组的首地址,所有只需要写数组名即可,不需要加&符号。char *string = "I love China";是把字符串第一个元素的地址赋给字符指针变量string。在用%s输出字符串时候,如:printf("%s\n", string)在输出项中给出字符指针变量名string,系统会输出string所指向的字符串第一个字符,然后自动使string加1,使指向下一个字符,在输出该字符........如此直到遇到字符串结束标志“\0”为止。注意:在内存中,字符串的最后被自动加了一个“\0”,因此在输出时能确定输出的字符到何时结束。
8. 如果p指向一个结构体变量stu,以下3种方法等价:
① stu.成员名(如 stu.num);
② (*p).成员名 ((*p).num);
③ p -> 成员名 (如 p -> num);
#include <stdio.h> typedef struct { int age; char *name; } Student; typedef Student * StuP; int main() { Student stu; StuP p = &stu; // p是一个指向结构体变量stu的指针 // 指针变量访问结构体成员用的是->,不是. p->age = 10; // 相当于 (*p).age = 10; //p.age = 10; p->name = "rose"; //p.name = "rose"; return 0; }