[英]Struct doesn't return correct value
当我在调试模式下运行代码时,可以看到来自以下各项的正确值:
createBooking("SOME NAME", 1, 2, 3, 4, 5, 6, 7);
发送到我的结构。 但是,当我尝试在结构体中调用某些东西时,我看不到该值。 printf
只打印0,而不是应该打印2。 我在main()
中的printf
中缺少什么吗? 还是什么呢? 我的老师一直在看代码,他也找不到问题。
main.c中:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main() {
struct Booking booking;
createBooking("SOME NAME", 1, 2, 3, 4, 5, 6, 7);
printf("%d", booking.pNumber);
getchar();
return 0;
}
functions.c:
#include "functions.h"
struct Booking createBooking(char *aName, int aPNumber, int aStartWeek, int aStopWeek,
int aCabNr, int aCabType, int aLiftcard, double aTotCost)
{
struct Booking booking = *(struct Booking*)malloc(sizeof(struct Booking));
strncpy(booking.name, aName, strlen(aName) + 1);
booking.pNumber = aPNumber;
booking.startWeek = aStartWeek;
booking.stopWeek = aStopWeek;
booking.cabNr = aCabType;
booking.cabType = aCabType;
booking.liftCard = aLiftcard;
booking.totCost = aTotCost;
return booking;
}
functions.h:
#ifndef functions_h
#define functions_h
#include <stdio.h>
#include <string.h>
typedef struct Booking {
char name[30];
int pNumber;
int startWeek;
int stopWeek;
int cabNr;
int cabType;
int liftCard;
double totCost;
} booking;
struct Booking createBooking(char *aName, int aPNumber, int aStartWeek, int aStopWeek,
int aCabNr, int aCabType, int aLiftcard, double aTotCost);
int bookBooking(struct Booking b);
#endif
因此,您可以这样声明struct:
struct Booking booking;
这就声明了一个具有未定义值的结构-在大多数编译器中为NULL / 0而不是调用createBooking来分配并返回一个新结构。 而您忽略了返回值:
createBooking("SOME NAME", 1, 2, 3, 4, 5, 6, 7);
比您打印未分配的结构:
printf("%d", booking.pNumber);
当然,它会返回0或意外的值。 您需要做的是:
struct Booking booking = createBooking("SOME NAME", 1, 2, 3, 4, 5, 6, 7);
printf("%d", booking.pNumber);
正如MM注释中正确指出的那样,您无需在函数内部进行动态分配,您所需要做的就是:
struct Booking createBooking(char *aName, int aPNumber, int aStartWeek, int aStopWeek, int aCabNr, int aCabType, int aLiftcard, double aTotCost)
{
struct Booking booking;
strncpy(booking.name, aName, strlen(aName) + 1);
booking.pNumber = aPNumber;
booking.startWeek = aStartWeek;
booking.stopWeek = aStopWeek;
booking.cabNr = aCabType;
booking.cabType = aCabType;
booking.liftCard = aLiftcard;
booking.totCost = aTotCost;
return booking;
}
您的函数createBooking
应该从堆中分配一个预订,并返回一个指向它的指针。 您还可以使用函数initBooking
,该函数接受指向预订的指针以及用于初始化预订的值。
这是两种方式的示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Booking *initBooking(struct Booking *bp,
char *aName, int aPNumber, int aStartWeek, int aStopWeek,
int aCabNr, int aCabType, int aLiftcard, double aTotCost)
{
if (bp) {
size_t len = strlen(aName);
if (len >= sizeof(bp->name))
len = sizeof(bp->name) - 1;
memcpy(bp->name, aName, len);
bp->name[len] = '\0';
bp->pNumber = aPNumber;
bp->startWeek = aStartWeek;
bp->stopWeek = aStopWeek;
bp->cabNr = aCabType;
bp->cabType = aCabType;
bp->liftCard = aLiftcard;
bp->totCost = aTotCost;
}
return bp;
}
struct Booking *createBooking(char *aName, int aPNumber, int aStartWeek, int aStopWeek,
int aCabNr, int aCabType, int aLiftcard, double aTotCost)
{
return initBooking(malloc(sizeof(struct Booking),
aName, aPNumber, aStartWeek, aStopWeek,
aCabNr, aCabType, aLiftcard, aTotCost);
}
int main() {
struct Booking booking;
initBooking(&booking, "SOME NAME", 1, 2, 3, 4, 5, 6, 7);
printf("%d", booking.pNumber);
struct Booking *bp;
bp = createBooking(&booking, "SOME NAME", 1, 2, 3, 4, 5, 6, 7);
printf("%d", bp->pNumber);
getchar();
return 0;
}
打开/打开编译器警告,它将为您提供很多帮助。 您有许多错误:
如果启用了警告,则编译器可以捕获所有这些信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.