27.字符串常量和文字常量区


字符串常量

#include
int main()
{
	//1.每个字符串都是一个地址,这个地址是字符串首元素的地址
	//2.字符串常量放在data区,文字常量区
	printf("s1 = %s\n", "hello mike");
	printf("s1 = %p\n", "hello mike");
	printf("s1 = %s\n", "hello mike" + 1);//跳过第一个元素
	printf("s4 = %c\n", *("hello wprld"));
}

文字常量区(不允许修改)

#include
#include
int main()
{
	//1.字符串常量就是此字符串的首地址元素
	printf("s1 = %p\n", "hello mike");
	char* p5;
	p5 = "helllo world";
	char* p2;
	printf("p2 = %p\n", "hello world");
	//2.字符串常量,文字常量区的字符串只读,不能修改
	printf("p1 = %c\n", *p2);
	//3.p1指向字符串常量,字符串常量为只读
	//*p1 = 'a';修改,error

	/*
	char* p3 = "hello";
	strcpy(p3, "abc");  p3指向文字常量区,不可以修改
	*/
	//1.p指针保存了"hello"的地址,指针所指向的内存不可以修改
	char* p = "hello";
	//2.把"hello"一个一个字符的放在buf数组中,数组的元素是可以修改的
	char buf[] = "hello";

	char* q[] = { "hello", "abc", "mike" };
	int n = sizeof(q) / sizeof(*q);
	for (int i = 0; i < n; i++)
	{
		printf("%s\n", p[i]);
	}
	
}