现在的位置: 首页 > 综合 > 正文

str[] , *str , static str[]的区别

2018年03月20日 ⁄ 综合 ⁄ 共 304字 ⁄ 字号 评论关闭
#include <stdio.h>

//str[]存在于getStr1的栈中,函数返回后就不存在了
char* getStr1()
{
	char str[] = "hello world";
	return str;
}

//*str存在于全局变量区中
char* getStr2()
{
	char* str = "hello world";
	return str;
}

//static变量存在于全局变量区中
char* getStr3()
{
	static char str[] = "hello world";
	return str;
}



int main()
{
	char* p = getStr3();//只有getStr1不能满足要求
	printf("%s\n",p);

	return 0;
}

抱歉!评论已关闭.