69,369
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
typedef struct test{
int a;
int b;
}test;
typedef struct test2{
int a;
int *b;
}test2;
test get(){
test t={3,4};
return t;
}
test2 get2(){
int a=8;
test2 t={7,&a};
return t;
}
int main(void){
int a=6;
test t1={1,2};
test t2=get();
test2 t3={5,&a};
test2 t4=get2();
printf("t1 %d %d\n",t1.a,t1.b);
printf("t2 %d %d\n",t2.a,t2.b);
printf("t3 %d %d\n",t3.a,*t3.b);
printf("t4 %d %d\n",t4.a,*t4.b); //*t4.b不对
return 0;
}
#include <stdio.h>
typedef struct{
int a;
int *b;
}t;
t get(){
int c=2;
t tt={1,&c};
return tt;
}
main(){
t t1=get();
printf("%d %d",t1.a,*t1.b);//这里会正确输出
return 0;
}
int main(void)
{
int a=6;
test t1={1,2};
test t2=get();
test2 t3={5,&a};
test2 t4=get2();
printf("t1 %d %d\n",t1.a,t1.b);
printf("t2 %d %d\n",t2.a,t2.b);
printf("t3 %d %d\n",t3.a,*t3.b);
printf("t4 %d %d\n",t4.a,*t4.b);
free(t4.b);//这里已经free了..........
return 0;
}
#include<stdio.h>
typedef struct test
{
int a;
int b;
}test;
typedef struct test2
{
int a;
int *b;
}test2;
test get()
{
test t={3,4};
return t;
}
test2 get2()
{
test2 t;
t.a = 7;
t.b = (int *)malloc(sizeof(int)); //分析在堆中
*(t.b) = 8;
return t;
}
int main(void)
{
int a=6;
test t1={1,2};
test t2=get();
test2 t3={5,&a};
test2 t4=get2();
printf("t1 %d %d\n",t1.a,t1.b);
printf("t2 %d %d\n",t2.a,t2.b);
printf("t3 %d %d\n",t3.a,*t3.b);
printf("t4 %d %d\n",t4.a,*t4.b);
free(t4.b);
return 0;
}
test2 get2(){
int * a=new int(8);
test2 t={7,a};
return t;
}
//只针对此题,一般是要写个拷贝构造函数的.