二级指针的技术推演——20200211

代码如下一:

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void modifyp1(char** p)
{
	*p = 900;
	printf("p1=%d\n", *p);
}

int main()
{
	int* p1 = NULL;
	int** p2 = NULL;
	//直接修改p1的值.
	p1 = 0xaa11;
	printf("p1=%08x\n", p1);
	//间接修改p1的值
	p2 = &p1;
	*p2 = 200;
	printf("p1=%d\n", p1);
	{
		*p2 = 400;
		printf("p1=%d\n", p1);
	}
	modifyp1(&p1);
	printf("p1=%d\n", p1);
	system("pause");
	return 0;
}

代码如下二:

#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int getStr2(char** myp1, int* mylen1, char** myp2, int* mylen2)
{
	int ret = 0;
	if (myp1 == NULL || myp2 == NULL || mylen1 == NULL || mylen2 == NULL)
	{
		ret = -1;
		printf("NULL error:%d\n", ret);
		return ret;
	}
	char* tmp1;
	tmp1 = (char*)malloc(100);
	char* tmp2;
	tmp2 = (char*)malloc(200);
	strcpy(tmp1, "123456789");
	strcpy(tmp2, "abcdefg");
	//
	*mylen1 = strlen(tmp1);
	*mylen2 = strlen(tmp2);
	//
	*myp1 = tmp1;
	*myp2 = tmp2;
	return ret;
}

int main()
{
	int ret = 0;
	char* p1 = NULL;
	char* p2 = NULL;
	int len1 = 0;
	int len2 = 0;
	ret = getStr2(&p1, &len1, &p2, &len2);
	if (ret != 0)
	{
		printf("func getStr2() error:%d\n", ret);
		return ret;
	}
	printf("p1=%s,p1 strlen=%d\n", p1, len1);
	printf("p2=%s,p2 strlen=%d\n", p2, len2);
	if (p1 != NULL)
	{
		free(p1);
		p1 = NULL;
	}
	if (p2 != NULL)
	{
		free(p1);
		p2 = NULL;
	}
	system("pause");
	return 0;
}
发布了140 篇原创文章 · 获赞 26 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41211961/article/details/104260844