结构体指针可以作为函数返回值类型,前提该指针必须指向堆内存,不能指向栈内存。返回结构体指针提高效率
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct myself {
char name[10];
int age;
int data;
};
struct myself * my(struct myself *aa) {
struct myself *ww = (struct myself*)malloc(sizeof(struct myself));
printf("%p\n", ww);
struct myself mm;
printf("%p\n", &mm);
printf("%p\n", aa);
strcpy(ww->name, aa->name);
ww->age = aa->age;
ww->data = aa->data;
return ww;
}
void main() {
struct myself my1;
//my->name = "sss";
strcpy(my1.name, "sss");
my1.age = 10;
my1.data = 20;
struct myself *myy = &my1;
struct myself *xx=my(myy);
//struct myself *my2=&my1;
//struct myself bb= my(my2);
printf("%s,%d,%d\n", xx->name, xx->age, xx->data);
getchar();
}