字符串是字符的集合,可以用数组(指针常量)或指针变量来表示。
这里主要注意所占空间的不同。以及字符串长度的计算函数的计算原理。
char *ptr="hello c!";
char ch[10]="hello c!";
下面给出一个实例:
#include<stdio.h>
#include<string.h>
int main(void){
char *ptr="hello c!";
char ch[10]="hello c!";//数组最少9个 hello c!+\0
printf("size is %d %d\n",sizeof(ptr),sizeof(*ptr));
printf("size is %d\n",sizeof(ch));
printf("size is %d %d\n",strlen(ptr),strlen(ch));
//几个字符串函数
//strlen(condt char*)//字符串长度
//strcpy(char*,const char*)//字符串复制
//strcat(char*,const char*)//字符串连接
//strcmp(const char*,const char*)//字符串比较
//strchr(const char*,int)//寻找int字符在字符串中第一次出现的位置
//stricmp(const char*,const char*)//字符串忽略大小写比较
return 0;
}