C语言 第 6 节 const char* 与 char* const

#include 

int main()
{
	char* plane = "asd";  // plane为指针(因为有*号 看到*号就代表着plane是指针)  char决定着指针所指地址处存放的数据类型 指向"asd"为常量字符串,所以不能修改
	//plane[0] = 's';  error 因为plane指向的是常量 所以不能修改  
	printf("plane = %s\n",plane);
	
	
	plane = "cd";   // plane 指向字符串常量"cd"  
 	printf("plane = %s\n",plane);
	
	const char* apple = "asd"; //const在指针前 ,意味着指针指向的内容不能变 但是可以改变指针所指的地址
	printf("apple = %s\n",apple);
	//apple[1] = 'e'; 指针指向的内容不能改 error: read-only variable is not assignable
	apple = "cde"; // 指针指向的地址可以改动
	printf("apple = %s\n",apple);
	
	char* const ble = "ade";// 这里const后面是ble 表示指针指向的地址不能变
	printf("ble = %s\n, ble[1] = %c\n", ble, ble[1]);
	//ble = "des"; error 指针的指向不能变
	//ble[2] = 'r';
	
	return 0;
}


输出:
plane = asd
plane = cd
apple = asd
apple = cde
ble = ade
, ble[1] = d


你可能感兴趣的:(C语言,C语言学习)