通过数组和指针操作字符串
作者:
arkliu | 来源:发表于
2022-10-05 13:32 被阅读0次#include <stdio.h>
#include <string.h>
/*c语言没有字符串类型,通过字符数组模拟
c语言字符串以字符'\0'结尾*/
int main() {
char buf[] = "hello world";
// []
for (size_t i = 0; i < strlen(buf); i++)
{
printf("%c", buf[i]);
}
printf("\n");
// 指针操作字符串
char * p = NULL;
p = buf;
for (size_t i = 0; i < strlen(buf); i++)
{
printf("%c", *(p+i));
p++;
}
printf("\n");
// buf和p完全等价吗?
//p++;
//buf++; //error: lvalue required as increment operand buf是一个常量,不能修改,改变后,释放内存会有问题
return 0;
}
本文标题:通过数组和指针操作字符串
本文链接:https://www.haomeiwen.com/subject/aeteartx.html
网友评论