---
title: "字符串指针、字符数组的sizeof和strlen结果"
author: "standalone"
date: "2010-09-27T08:36:13Z"
tags: []
categories: ["编程语言"]
slug: "sizeof-strlen"
draft: false
lastmod: "2010-09-27T08:42:24Z"
id: 772764
url: "https://standalone.iteye.com/blog/772764"
---

```
int main(){
	
	char *p1="china";
	char p2[]="china";
	char p3[100]={0};
	strcpy(p3, "china");
	cout << sizeof(p1) << endl;
	cout << sizeof(p2) << endl;
	cout << sizeof(p3) << endl;
	cout << strlen(p1) << endl;
	cout << strlen(p2) << endl;
	cout << strlen(p3) << endl;
}
```

 在vc上面的输出结果：

 

4

6

100

5

5

5

 

解释：sizeof(p1)是一个指针的大小；sizeof(p2)和sizeof(p3)是数组的大小，包括'\\0'，strlen则不包括结尾的'\\0'。