二维数组 字符串和指针
指针+1移动了相当于所指向类型的大小的字节
![]()
![]()
![]()
![]()
![]()
![]()
int *s1[100]
移动了4个字节
int (*s2)[100]
移动了400个字节
char *s3
移动了1 个字节
int *s4
移动了4个字节
***p2如何理解?

int *p0 = &i
*p0 = i
int **p1 = &p0
**p1 = i
int ***p2 = &p1
***p2 = i
*p2 = p1的值
**p2 = p0的值
***p2 = i的值
所以***p2就是p0的值 而p0的值最后指向了p2的地址
即***p2指向了p2的地址
二维数组 + 数组指针 + 二级指针
int a[3][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } };//二维数组int(*p)[4] = a; //数组指针printf("%d\n", p); //8322788 这是个第一行的行地址,类似于二级指针printf("%d\n", *p); //8322788 这是个第一行第一列的地址printf("%d\n", **p); //0 这是第一个值
*(*(p+j)+i)
p是第一行的行地址,
二维数组里的 *(p+j)是个指针地址
ps :参数里
二维数组等价于数组指针
二级指针等价于指针数组
二维数组传参:
按列优先打出来数组内容
#include <stdio.h>int main(){int a[3][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } };int(*p)[4] = a;for (int i = 0; i < 4;i++){for (int j = 0; j < 3; j++){printf("%d ",*(*(p+j)+i));}printf("\n");}return 0;}
二维数组传参要传两个维度

const:
如果不改变值,尽量使用const
回调函数:
把函数的指针作为一个函数的参数,叫做回调函数
strcpy :
#include <stdio.h>#include <string.h>void str_cpy( char * dest , const char * src){while(*dest++ = *src++);}int main(){char buf[100] = "aaaaaaaaaaaaaaaaaaaaaaaa"; //用来测试是否添加\0str_cpy(buf , "liuwei");printf("%s\n" , buf);return 0;}

strncpy:
- #include <stdio.h>
#include <string.h>void str_ncpy( char * dest , const char * src , int n){while( (*dest++ = *src++) && n--);*(dest-1) = 0;}int main(){char buf[5] = "aaaa";;str_ncpy(buf , "liuweinihao" , 6);printf("%s\n" , buf);return 0;}

字符串和指针
#include <stdio.h>int main(){int i;char s[100] = "a刘威b";printf("%d\n", s[0]);printf("%d\n", s[1]);printf("%d\n", s[2]);printf("%d\n", s[3]);printf("%d\n", s[4]);printf("%d\n", s[5]);return 0;}

汉字由两个字节构成
192 245 是刘
-63 -11
205 254 是威
-51 -2
自己写一个strlen 类似 mb_strlen 汉字只算一个字符
#include <stdio.h>#include <string.h>int mb_strlen(const char * p){int len = 0;while( *p ){len++;if(*p++ < 0)p++;}return len;}int main(){char *s = "a我b是c你d大e";int len = mb_strlen( s );printf("%d\n",len);return 0;}


浙公网安备 33010602011771号