结构中一个字段的值无意中成为另一个字段的值

时间:2014-11-10 06:45:49

标签: c struct

我正在尝试创建一个结构,该结构将携带餐馆订单的数据以进行送货上门。

这是我的头文件order.h

#ifndef ORDER_H
#define ORDER_H


extern long orderNumber;


typedef struct {

    long orderNumber;
    char *customerName;
    long customerContactNumber;
} *Order;



Order newOrder();
void setCustomerName(Order, char *);
void setCustomerContactNumber(Order, long);


#endif  

我的头文件中实现方法的源文件名为order.c

#include <stdlib.h>
#include "order.h"


long orderNumber = 0;


Order newOrder() {


    orderNumber++;

    Order order = (Order)malloc(sizeof(Order));
    order->orderNumber = orderNumber;

    return order;
}



void setCustomerName(Order order, char *customerName) {

    order->customerName = customerName;
}



void setCustomerContactNumber(Order order, long customerContactNumber) {

    order->customerContactNumber = customerContactNumber;
}  

我的包含main方法的文件是main.c

#include "order.h"
#include <stdio.h>



int main() {

    void displayOrder(Order);
    Order order1 = newOrder();
    Order order2 = newOrder();
    Order order3 = newOrder();

    setCustomerName(order1, "Aditya R.Singh");
    setCustomerContactNumber(order1, 123L);

    setCustomerName(order2, "Rameshwarpratap R.Singh");
    setCustomerContactNumber(order2, 456L);

    setCustomerName(order3, "Sarita R.Singh");
    setCustomerContactNumber(order3, 789L);

    displayOrder(order1);
    displayOrder(order2);
    displayOrder(order3);

    return 0;
}  




void displayOrder(Order order) {

    long id = order->orderNumber;
    char *name = order->customerName;
    long contactNumber = order->customerContactNumber;

    fprintf(stdout, "==========================================\n");
    fprintf(stdout, "Order number: %ld\n", id);
    fprintf(stdout, "Customer Name: %s\n", name);
    fprintf(stdout, "Customer Contact Number: %ld\n", contactNumber);
    fprintf(stdout, "==========================================\n");
    fprintf(stdout, "\n\n\n");

    fflush(stdout);
}  

预期输出为:

==========================================
Order number: 1
Customer Name: Aditya R.Singh
Customer Contact Number: 123
==========================================



==========================================
Order number: 2
Customer Name: Rameshwarpratap R.Singh
Customer Contact Number: 456
==========================================



==========================================
Order number: 3
Customer Name: Sarita R.Singh
Customer Contact Number: 789
==========================================  

但实际输出是:

==========================================
Order number: 1
Customer Name: Aditya R.Singh
Customer Contact Number: 123
==========================================



==========================================
Order number: 123
Customer Name: Rameshwarpratap R.Singh
Customer Contact Number: 456
==========================================



==========================================
Order number: 456
Customer Name: Sarita R.Singh
Customer Contact Number: 789
==========================================  

正如您所注意到的,orderNumber的{​​{1}}被order2 customerContactNumber覆盖。

order1的{​​{1}}被orderNumber的{​​{1}}覆盖。

为什么会发生这种情况? 我怎么能得到预期的输出?

有任何帮助吗?提前谢谢。

2 个答案:

答案 0 :(得分:3)

问题在于:

Order order = (Order)malloc(sizeof(Order));

您已将Order定义为

typedef struct {
  long orderNumber;
  char *customerName;
  long customerContactNumber;
} *Order;

是指针类型,前一个分配为一个指针分配空间。正确的分配应该是:

    Order order = (Order)malloc(sizeof(*order));

答案 1 :(得分:2)

您分配内存的行是错误的。

Order order = (Order)malloc(sizeof(Order));

sizeof(order)是指针的大小,而不是struct的大小。之后的一切都会受到不明确的行为。

要解决此问题,您可以使用:

Order order = (Order)malloc(sizeof(*order));

使用更好的typedef s可以避免这样的问题。而不是

typedef struct {
  long orderNumber;
  char *customerName;
  long customerContactNumber;
} *Order;

最好使用:

typedef struct {
  long orderNumber;
  char *customerName;
  long customerContactNumber;
} Order;

typedef Order* OrderPtr;

这些会减少对变量类型的混淆。