Struct不会返回正确的值

时间:2016-01-19 19:55:00

标签: c struct return

当我在调试模式下运行代码时,我可以看到来自的正确值:

createBooking("SOME NAME", 1, 2, 3, 4, 5, 6, 7);

发送到我的结构。但是当我尝试在我的结构中调用某些东西时,我看不到它的价值。 printf只是打印0而不是2。我printf中的main()是否遗漏了这些内容?或者它会是什么?我的老师一直在看代码,他也找不到问题。

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

3 个答案:

答案 0 :(得分:0)

所以你像这样声明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);  

正如在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;
}

答案 1 :(得分:0)

您的函数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;
}

答案 2 :(得分:0)

打开/关闭编译器警告,它会给你很多帮助。你有很多错误:

  1. 在createbooking中分配指针并引用它而不间接。
  2. 在不测试输入大小的情况下写入固定长度的char [30]缓冲区。
  3. 使用预设设置字段。而不是 - &gt;指针符号。
  4. 在将返回值声明为值类型(整个结构)时返回预订(指针)。
  5. 忽略createBooking的返回值。
  6. 如果您有警告,编译器可以捕获所有这些内容。