strlen(x) 返回 size_t 类型,size_t是 unsigned int 类型,所以 strlen(x)-strlen(y) 返回 unsigned int 始终 >=0
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h>#include <string.h>void main(){char *sa="sdhshdh";char *sb="cdehhhhsdssssd";printf("%d , %d \n",strlen(sa),strlen(sb)); if(strlen(sa)-strlen(sb)>=0){ printf("run 1\n");}} |

如果不加 include <string.h>头文件
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h>void main(){char *sa="sdhshdh";char *sb="cdehhhhsdssssd";printf("%d , %d \n",strlen(sa),strlen(sb)); if(strlen(sa)-strlen(sb)>=0){ printf("run 1\n");}} |
1 | warning C4013: 'strlen' undefined; assuming extern returning int |

2可变参数列表
要用到 stdarg 宏
3数组名
数组名是一个常数指针,在两种情况下数组名不用 指针常数表示
sizeof sizeof返回整数数组的长度
& 是指向数组的指针,而不是指向指针常量的指针。
1 2 3 4 5 6 | #include <stdio.h>void main(){int a[4]={1,1,1,1};printf("%d \n",sizeof(a));} |

并不是 4(指针常量的长度)
1 2 3 4 5 6 7 8 | #include <stdio.h>void main(){int a[4]={1,1,1,1};int *b;b=a;printf("%d \n",b[2]);} |
数组对数组赋值的方法:利用循环(已经有了数组,对数组进行更新)
利用指针变量。
不能这样写
1 2 3 | int a[4]={1,1,1,1};int b[4];b=a; |
因为 b为指针常量不能赋值。
4下标引用,和间接引用
1 2 3 4 5 6 7 | #include <stdio.h>void main(){int a[4]={1,2,3,4};printf("%d \n",*(a+2));printf("%d \n",a[2]);} |

负值下标
1 2 3 4 5 6 7 8 9 | #include <stdio.h>void main(){int a[4]={1,2,3,4};int *p;p=a+2;printf("%d \n",*(a+1));printf("%d \n",p[-1]);} |

下标引用可以作用于任何指针
1 2 3 4 5 6 7 8 9 | #include <stdio.h>#include <math.h>void main(){int a[4]={1,2,3,4};printf("a[0]->%d \n",a[0]);printf("1[a]->%d \n",1[a]);printf("a[1]->%d \n",a[1]);} |

浙公网安备 33010602011771号