02 09 指针做函数参数的输入特性;02 10 指针做函数参数的输出特性

02 09 指针做函数参数的输入特性
02 10 指针做函数参数的输出特性

函数A调函数B,A是主调函数,B是被调函数
输入特性:A开辟堆栈,B读写堆栈
输出特性:B开辟堆栈,A读写堆栈

01 输入特性:读堆区字符串

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

//1、char* str
void printString1(const char* str)
{
	printf("%s\n", str);
	//1汉字==2B,short==2B,总结就是读1*2B的数据
	printf("%s\n\n", (short*)str+1);
}

void test1()
{
	//(char*)明白这块内存存的数据类型
	char* pstr = malloc(100 * sizeof(char*));
	//堆区
	memset(pstr, 0, 100);
	//字符串常量区拷贝到堆区
	strcpy(pstr, "公孙五楼");
	printString1(pstr);
}

void main() {
	test1();

	system("pause");
}

在这里插入图片描述

01 输入特性:读栈区字符串数组

不知道这里有没有字符串常量区的存在,
堆区中数据的排列也是一个单元一个单元地叠上去
在这里插入图片描述

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

//2、char* strs[]
//void printStrings2(char* strs[],int len)
void printStrings2(char** pp,int len)
{
	printf("%d\n", len);
	for (int i = 0; i < len; i++ ) {
		printf("%s\n", pp[i]);
	}
}

void test2()
{
	char* p[] = { "aa","bb","cc","dd" };
	int len = sizeof(p) / sizeof(p[0]);
	printStrings2(p,len);
}

void main() {

	test2();
	system("pause");
}

在这里插入图片描述

02 输出特性:堆区字符串

在这里插入图片描述

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

void allocateSpace(char** pp)
{
	char* temp = malloc(100);
	memset(temp, 0, 100);
	strcpy(temp, "胡藩");
	//字符串常量的地址
	printf("%p\n", &"胡藩");
	*pp = temp;
}
void test1()
{
	char* p = NULL;
	allocateSpace(&p);
	printf("%s\n", p);

	if (p != NULL) {
		free(p);
		p = NULL;
	}
}

void main() {
	test1();

	system("pause");
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值