当我在调试模式下运行我的代码时,我可以看到正确的值来自:
createBooking("SOME NAME", 1, 2, 3, 4, 5, 6, 7);被送到我的结构里。但是当我试图在我的结构中调用一些东西时,我看不到它的价值。printf只是打印0,而不是像它应该的那样打印2。是我在printf in 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;
}职能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;
}功能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发布于 2016-01-19 20:06:33
因此,您可以这样声明结构:
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); 正如M.M .的注释中所正确指出的,您不需要在函数中执行动态分配,只需做以下工作:
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;
}发布于 2016-01-19 23:44:19
您的函数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;
}发布于 2016-01-19 23:50:10
打开/向上打开编译器警告,它将为您提供大量帮助。您有许多bug:
如果您有警告,所有这些都可以被编译器捕获。
https://stackoverflow.com/questions/34885793
复制相似问题