- 指针与字符串
%c 打印一个字符,要的是该字符的地址
%s 打印一个字符串,要的是字符串首个字符的地址
int main()//指针与字符串
{
//定义字符串
char a[] = { "helloworld" };//定义一个字符数组,其内容为:helloworld\0
//定义一个指针用来保存数组首元素的地址
char *p = a;
printf("%c\n", *(p + 3));// %c 打印一个字符,要的是该字符的地址
printf("%s\n", p);// %s 打印一个字符串,要的是字符串首个字符的地址
printf("%s\n", p + 2);// 表示从指定字符开始打印字符串
printf("%d\n", sizeof(p)); //指针大小为 4
printf("%d\n", sizeof(a)); //数组大小为 11
printf("%d\n", strlen(p)); // 有效字符个数数组大小为 10
printf("%d\n", strlen(a)); // 有效字符个数数组大小为 10
*p = 'm';//将 p指向 的内存中的内容改为‘m’
printf("%s\n", p);// melloworld
p++;//将指针p往后移一个元素,打印时就是从p指向的位置向后打印,因此打印结果中没有‘m’
*p = 'k';
printf("%s\n", p);// klloworld
system("pause");
return 0;
}
- 字符指针作为形参
char *my_strcat(char *src,char *dst)
{
int i = 0;
int n = strlen(src);
while (*(dst + i) != '\0')
{
*(src + n + i) = *(dst + i); // 等价与 src[n + i] = dst[i];
i++;
}
*(src + n + i) = '\0'; //最后给字符串补上结束符'\0'
return src;
}
int main()//字符指针作为形参
{
char str1[128] = "hello";
char str2[128] = "123456";
//my_strcat(str1, str2);
printf("%s\n", my_strcat(str1, str2));
system("pause");
return 0;
}
- const修饰的指针变量
int main()//const的使用
{
//const修饰一个变量,表示该变量为只读
const int a = 10;
//a=100;//err 不能重新给a赋值
//指针变量 & 指针指向的内存
char buf[] = "dfhoiasfdjbklasdnf";
const char *p1 = buf; // const修饰的是指针,表示不能通过 *P1 修改指向的内容
//p1[1] = 'h';//err
p1 = "123456";//OK
char * const p2 = buf;// const修饰 变量p2本身,表示不能更改指针p的指向
p2[1] = 'h';//ok
//p2 = "123456";//err
system("pause");
return 0;
}
- 指针字符数组
int main()//字符指针数组
{
//char *p1 = "heihei";
//char *p2 = "haha";
//char *p3 = "xixi";
//char *num[3] = { p1,p2,p3 };
char *num[3] = { "heihei","haha","xixi" };//将后面三个元素的地址存在num数组里,并不是将三个字符串存在数组中
for (int i = 0; i < 3; i++)
{
printf("%s\n", num[i]);//打印三个字符串
}
printf("%c\n", *num[0]);//打第一个字符串的第一个字符 h
printf("%c\n", *(num[1] + 1));//打第二个字符串的第二个字符 a
printf("%c\n", *(num[2] + 2));//打第三个字符串的第三个字符 x
//定义一个指针,保存num数组首元素地址 p+i == &num[i]; *(p+i) <==> num[i]
char **p = num;
for (int i = 0; i < 3; i++)
{
//printf("%s\n", p[i]);//与下一句等价
printf("%s\n", *(p+i));//通过二级指针p 打印三个字符串
}
printf("%c\n", **p);//打第一个字符串的第一个字符 h
printf("%c\n", *(*(p+1) + 1));//打第二个字符串的第二个字符 a
printf("%c\n", *(*(p+2) + 2));//打第三个字符串的第三个字符 x
//注意 : *(*(p+1) + 1) <==> *(p[1]+1)<==> p[1][1]
system("pause");
return 0;
}
字符指针作为main()函数的形参
int argc : 执行可执行程序的参数个数
int *argv[] : 是一个字符指针数组,保存的是参数(字符串)的首元素地址
int main(int argc,int *argv[])
{
printf("%d\n", argc);
printf("%s\n", argv[0]);
printf("%s\n", argv[1]);
system("pause");
return 0;
}
- 字符串处理函数
int main()//常用字符串处理函数
{
char p1[128] = "heihei";
char p2[128] = "haha";
//常用字符串库函数
strcpy_s(p1,p2);
strncpy_s(p1, p2, 2); //将p2中的前2个字符拷贝到p1中,如果遇到'\0'拷贝结束
strcat_s(p1, p2);
strncat_s(p1, p2, 3);//将p2中的前2个字符连接到p1后,如果遇到'\0'结束
strcmp(p1, p2);
strncmp(p1, p2, 3);//比较p1 p2中的字符串前3个字符,遇到'\0'结束
int year = 2022;
int month = 4;
int day = 6;
char buf[1024] = "";
//printf("year = %d month = %d day = %d",year,month,day");//普通输出
int len = sprintf_s(buf, "year=%d month=%d day=%d",year,month,day);//组包函数 sprintf_s(buf,"格式","数据")将数据按照格式组包,存放在buf中,返回值为组包后的长度
printf("buf[%s]\n", buf);//组包函数输出:指定格式从buf里输出
printf("%d\n", len);//组包长度
int hour = 0;
int min = 0;
int sec = 0;
//scanf("%d:%d%:d", &year, &month, &day);//从键盘获取数据
//printf("year = %d month = %d day = %d",year,month,day);
char buf2[1024] = "2020:04:06";
sscanf_s(buf2, "%d:%d%:d", &hour, &min, &sec);//拆包函数:从 buf 中获取数据。 sscanf_s(buf,"格式","数据")将数据按照格式拆包
printf("%d %d %d\n", hour, min, sec);
system("pause");
return 0;
}