c语言 参数传值和传地址

static void TestCharP(char **p)
{
	char *q = "ssssss";
	*p=q;
}

static void TestCharP1(char *p)
{
	char *q = "ssssss";
	p=q;
}

static void TestInt(int *a)
{
	*a = 5;
}

static void TestInt1(int a)
{
	a = 5;
}

static void TestBuf(char buf[])
{
	buf[0] = 'a';
}


//传值和传地址的区别
int main()
{
	int a = 0;
	int a1 = 0;
	char *p=NULL;
	char buf[5] = {0};
	char *p1 = NULL;

	TestInt(&a);
	printf("%d\n",a);

	TestInt1(a1);
	printf("%d\n",a1);

	TestCharP(&p);
	printf("%s\n",p);

	TestCharP1(p1);
	printf("%s\n",p1);

	TestBuf(buf);
	printf("%s\n",buf);

	return 0;
}

输出:

 

2.查看地址转换

static void TestCharP(char *p)
{
	//p指向地址:0x00045860
	char *q = "ssssss";
	//q指向地址:0x00045858
	p=q;
	//p指向地址:0x00045858
}


//传值和传地址的区别
int main()
{
	char *p="aaa";
	//p指向地址:0x00045860
	TestCharP(p);
	//p指向地址:0x00045860
	printf("%s\n",p);

	return 0;
}

查看 p指向地址没有改变

static void TestCharP(char **p)
{
	//*p指向地址:0x0014f888
	char *q = "ssssss";
	//q指向地址:0x01185858
	*p=q;
	//*p指向地址:0x01185858
}


//传值和传地址的区别
int main()
{
	char *p="aaa";
	//p指向地址:0x01185860
	TestCharP(&p);
	//p指向地址:0x00045858
	printf("%s\n",p);

	return 0;
}

  查看 p指向地址改变

 

posted @ 2017-04-19 10:23  翻白眼的哈士奇  阅读(3628)  评论(0编辑  收藏  举报