新建项目
新建源文件
关闭scanf_s和printf_s检查:project -> 属性 -> C/C++ -> 常规 ->SDL:否


char* str="..."出现”"const char *" 类型的值不能用于初始化 "char *" 类型的实体“错误:project -> 属性 -> C/C++ -> 语言 -> 符合模式:否


调试
指针的移动
#include
#include
int main() {
char* str = "hello world";
char* point = str;
for (int i = 0; i < strlen(str); i++) {
printf("%p:%c", point, *point);
point++;
}
return 0;
}
%s#include
#include
#include
int main() {
//char* str;//直接这样不行
char* str = (char*)malloc(sizeof(char)*30);
scanf("%s", str);//不用取地址符了,str指向的就是一片地址空间
printf("%s\n", str);
char str1[30];
printf("%d %d", sizeof(str1),sizeof(str));//30 8
return 0;
}
#include
#include
int main() {
char *str = "hello world!";
printf("str:%s,strlen:%d,sizeof:%d\n", str,strlen(str),sizeof(str));
//str:hello world!,strlen:12,sizeof:8
printf("%p\n", str);//00007FF72DB59C28
printf("%c\n", *str);//h
printf("%p\n", str+1);//00007FF72DB59C29
printf("%c\n", *(str+1));//e
return 0;
}
在C语言中,使用string.h头文件中的 strlen() 函数来求字符串的长度。
在C语言中,字符串总是以’\0’作为结尾,所以’\0’也被称为字符串结束标志,或者字符串结束符。由" "包围的字符串会自动在末尾添加’\0’。
#include
#include
int main() {
char str1[30] = { 'h','e','l','l','o' };
printf("%s", str1);//hello
char str2[30];
str2[0] = 'h';
str2[1] = 'e';
str2[2] = 'l';
str2[3] = 'l';
str2[4] = 'o';
printf("%s", str2);//hello烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫虜悿f?
return 0;
}
注意!字符数组只有在定义时才能将整个字符串一次性地赋值给它,一旦定义完了,就只能一个字符一个字符地赋值了。
char str1[] = "hello world"char str1[] = {"hello world"}char str2[30] = "hello world"char str2[30] = {"hello world"}char str1[] = "hello world";
//char str1[] = {"hello world"};
printf("str:%s, strlen:%d, sizeof:%d\n",str1,strlen(str1),sizeof(str1));
//str:hello world, strlen:11, sizeof:12
char str2[30] = "hello world";
//char str2[30] = {"hello world"};
printf("str:%s, strlen:%d, sizeof:%d\n",str2,strlen(str2),sizeof(str2));
//str:hello world, strlen:11, sizeof:30
// char str[30];
// str = "hello world";
// 哒咩
#include
#include
int main() {
char str[] = "hello";
char* point = str;
for (int i = 0; i < strlen(str); i++)
printf("%c %c %d\n", str[i], *(point + i), str[i] == *(point + i));
/*
h h 1
e e 1
l l 1
l l 1
o o 1
*/
return 0;
}


char *match( char *s, char ch1, char ch2 ){
int sign = 0;
char* c = s;
char* p = "";
while(*c != '\0'){
if(!sign&&*c == ch1) {
sign = 1;
p = c;
}
if(sign) printf("%c",*c);
if(*c == ch2) break;
c++;
}
printf("\n");
return p;
}