函数在操作,在栈上进行,形参的拷贝和函数的运行,基本上都在栈上完成,所以结构体的传参,对栈区的资源消耗较大。而传地址的操作则会节省栈区资源,不需要形参的拷贝过程,而是直接寻址。
#define _CRT_SECURE_NO_WARNINGS 1
#include "stdio.h"
struct T
{
char t;
char s;
};
typedef struct Stu
{
char name[20];
int age;
char sex[5];
char tele[12];
struct T u;
}Stu;
void Print1(Stu tmp)
{
printf("name: %s\n",tmp.name);
printf("age: %d\n", tmp.age);
printf("sex: %s\n", tmp.sex);
printf("tele: %s\n", tmp.tele);
printf("t: %c\n", tmp.u.t);
printf("s: %c\n", tmp.u.s);
}
void Print2(Stu* ps)//此种方法打印更好,减少了数据压栈
{
printf("name: %s\n", ps->name);
printf("age: %d\n", ps->age);
printf("sex: %s\n", ps->sex);
printf("tele: %s\n", ps->tele);
printf("t: %c\n", ps->u.t);
printf("s: %c\n", ps->u.s);
}
int main()
{
struct T u = { 'w','e'};
Stu s1 = { "张三", 12, "男", "12583581451",{'w','e'}};
Print1(s1);
Print2(&s1);
return 0;
}
//int main()
//{
// char t = { 'w' };
// printf("%c\n", t);
// return 0;
//}
标签:tmp,ps,压栈,复习,打印,sex,char,tele,printf
From: https://blog.51cto.com/u_15906483/6717008