【C语言】sizeof常量字符串

常量字符串的常用表示方式:
1、常量自身;如:“abc”
2、将常量存放到数组;如:char []a = "abc"
3、用字符指针指向常量字符串;如:const char *pStr = "abc"

以上三种表示方式,通过sizeof计算出来值有自身的含义:
1、存放常量字符串的空间; sizeof("abc")  = 4
2、存放常量字符串使用的数组空间;sizeof(a) = 4
3、指向常量字符串的指针空间;sizeof(pStr) = 8 [64位系统]

#include 
#include 

int main()
{
	int l1 = sizeof("a");
	int l2 = sizeof("bb");
	int l3 = sizeof("ccc");
	printf("%d, %d, %d\n", l1, l2, l3);

	const char *p1 = "d";
	const char *p2 = "ee";
	const char *p3 = "fff";
	printf("%ld, %ld, %ld\n", sizeof(p1), sizeof(p2), sizeof(p3));

	char a1[] = "g";
	char a2[] = "hh";
	char a3[] = "iii";
	printf("%ld, %ld, %ld\n", sizeof(a1), sizeof(a2), sizeof(a3));

	return 0;
}

运行结果:
 

2, 3, 4
8, 8, 8
2, 3, 4

你可能感兴趣的:(总结,c++)