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");
}