个人对于字符指针知识的总结。欢迎指正,交流探讨
1.char* ps= "hello zsr";
本质上是将"hello zsr"字符串的首字符'h'的地址放在了变量ps中
printf("%c",*ps);//输出为h
printf("%s",ps);//输出为hello zsr
2.char* p = "hello zsr";//"hello zsr"为常量字符串
*p = 'w'; // 这里要改常量字符串的内容 这不正确 常量不可更改
printf("%s", p);
3.#include<stdio.h>int main() {
char str1[] = "hello zsr";
char str2[] = "hello zsr";
const char* str3 = "hello zsr";//const放在*左边,修饰的是指针指向的内容不能被修改
const char* str4 = "hello zsr";
if (str1 == str2) {
printf("st1 and str2 same\n");
}
else
printf("str1 and str2 not same\n");
if (str3 == str4) {
printf("str3 and str4 same\n");
}
else
printf("str3 and str4 not same\n");
}
str1str2为数组首元素的地址 在内存中 分别为str1和str2分配到了2个不同的内存 故str1!=str2
由于hello zsr是常量 不可以被修改 故只会在内存中出现一份 str3放的是h的地址,str4放的是h的地址 故str3==str4