关于C语言结构体指针的问题,求大神回答....

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

typedef struct goods_node{
char number[10];
float price;
int sum;
struct goods_node *next;
} goods;

typedef struct customer_node{
char name[20];
char address[20];
char Tel[20];
struct customer_node *next;
} customer;

typedef struct order_node{
char orderNumber[10];
goods *a;
customer *c;
int amount;
float pay;
char status[20];
struct order_node *next;
} order;

void main()
{
order *c1;
goods *c2;
customer *c3;
c1=(order*)malloc(sizeof(order));
c2=(goods*)malloc(sizeof(goods));
c3=(customer*)malloc(sizeof(customer));
printf("please inlput:\n");
scanf("%s%f%s",c1->a->number,&c1->a->price,c1->c->Tel);
printf("%5s%6.1f%5s",c1->a->number,c1->a->price,c1->c->Tel);
}
想通过order这个结构体指向里面的goods和customer这两个结构体的变量,但是输不出来,请大神解答.....谢谢

c1=(order*)malloc(sizeof(order));
   c2=(goods*)malloc(sizeof(goods));
   c3=(customer*)malloc(sizeof(customer));

下面添加

c1->a=(goods*)malloc(sizeof(goods));
   c1->c=(customer*)malloc(sizeof(customer));

因为你虽然为c1,c2,c3分配了空间,但是没有为c1中的good *a和customer *c分配空间

追问

谢谢你的回答,么么哒.....

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-04-03
void main()
{
order *c1;
goods *c2;
customer *c3;
c1=(order*)malloc(sizeof(order));
c2=(goods*)malloc(sizeof(goods));
c3=(customer*)malloc(sizeof(customer));
c1->a=c2;//结构体的成员指针没有初始化赋值,指向不明
c1->c=c3;
printf("please inlput:\n");
scanf("%s%f%s",c1->a->number,&(c1->a->price),c1->c->Tel);
printf("%5s %6.1f %5s",c1->a->number,c1->a->price,c1->c->Tel);
}
相似回答