gpt4 book ai didi

c - 结构中一个字段的值无意中变成了另一个字段的值

转载 作者:太空宇宙 更新时间:2023-11-04 05:56:22 25 4
gpt4 key购买 nike

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

这是我的名为 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.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
==========================================

如您所见,order2orderNumberorder1customerContactNumber 覆盖。

并且 order3orderNumberorder2customerContactNumber 覆盖。

为什么会这样?
我应该如何获得预期的输出?

有什么帮助吗?提前致谢。

最佳答案

问题出在这里:

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

您已将 Order 定义为

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

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

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

关于c - 结构中一个字段的值无意中变成了另一个字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26838214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com