#include <stdio.h>struct A
{
int a;
};struct B
{
int b;
};struct C
{
struct A* a1;
struct B* b1;
}struct A* a2()
{
struct A* a3;
int a = 3;
a3->a = 3;
return a3;
}struct B* b2()
{
struct B* b3;
int b = 4;
b3->b = 4;
return b3;
}void main()
{
struct C *c;
struct A* a4;
struct B* b4;
a4 = a2();
b4 = b2();
c->a1 = a4;
c->b1 = b4;
printf("%d%d\n",c->a1->a,c->b1->b);
printf("%d",a4->a);
}

解决方案 »

  1.   

    struct A* a2() 

    struct A* a3; 
    int a = 3; 
    a3->a = 3; //这里a3是野指针
    return a3; 
    } struct B* b2() 

    struct B* b3; 
    int b = 4; 
    b3->b = 4; //这里b3是野指针
    return b3; 
      

  2.   

    #include <stdio.h> struct A 

    int a; 
    }; struct B 

    int b; 
    }; struct C 

    struct A* a1; 
    struct B* b1; 
    } struct A* a2() 

    struct A* a3 = (struct A*)malloc(sizeof(struct A)); //分配内存
    int a = 3; 
    a3->a = 3; 
    return a3; 
    } struct B* b2() 

    struct B* b3 = (struct B*)malloc(sizeof(struct B)); //分配内存
    int b = 4; 
    b3->b = 4; 
    return b3; 
    } void main() 

    struct C *c = (struct C*)malloc(sizeof(struct C)); //分配内存
    struct A* a4; 
    struct B* b4; 
    a4 = a2(); 
    b4 = b2(); 
    c->a1 = a4; 
    c->b1 = b4; 
    printf("%d%d\n",c->a1->a,c->b1->b); 
    printf("%d",a4->a); //释放内存
    delete a4;
    delete b4;
    delete c;
    }
      

  3.   

    #include <stdio.h> 
    #include <stdlib.h> struct A 

    int a; 
    }; struct B 

    int b; 
    }; struct C 

    struct A* a1; 
    struct B* b1; 
    } ;struct A* a2() 

    struct A* a3 = (struct A*)malloc(sizeof(struct A)); //分配内存
    int a = 3; 
    a3->a = 3; 
    return a3; 
    } struct B* b2() 

    struct B* b3 = (struct B*)malloc(sizeof(struct B)); //分配内存
    int b = 4; 
    b3->b = 4; 
    return b3; 
    } void main() 

    struct C *c = (struct C*)malloc(sizeof(struct C)); //分配内存
    struct A* a4; 
    struct B* b4; 
    a4 = a2(); 
    b4 = b2(); 
    c->a1 = a4; 
    c->b1 = b4; 
    printf("%d%d\n",c->a1->a,c->b1->b); 
    printf("%d",a4->a); //释放内存
    delete a4;
    delete b4;
    delete c;
    }
      

  4.   

    #include <stdio.h> 
    #include <stdlib.h> struct A 

    int a; 
    }; struct B 

    int b; 
    }; struct C 

    struct A a1; 
    struct B b1; 
    } ;void a2(struct A* a3) 

    int a = 3; 
    a3->a = 3; } void b2(struct B* b3) 

    int b = 4; 
    b3->b = 4; } void main() 

    struct C c;a2(&c.a1); 
    b2(&c.b1); printf("%d%d\n",c.a1.a,c.b1.b); }