C - 如何将 realloc 与指针一起使用



在这种情况下,我在使用 realloc 时遇到了问题。我的编译器devC ++一直告诉我,这是一个无效的"sizeof"到不完整的类型"struct WaitlistEntry"的应用程序。我基本上是尝试使用给定的结构对结构进行 malloc,然后将数据存储在其中。

这是整个函数:

void attemptEnrollment( Course* pCourse, int iStudentID, int iPriority ){ 
if (pCourse->iNumEnrolled < pCourse->iMaxEnrolled)
enrollStudent(pCourse, iStudentID);
else {
WaitlistEntry w1;
w1 = (struct WaitlistEntry*)malloc(sizeof(struct WaitlistEntry));
w1.iStudentID = iStudentID;
w1.iPriority = iPriority;
waitlistStudent(pCourse,w1);
}
}

以下是结构:

typedef struct{
int iPriority;          /* Priority of the student to be enrolled */
int iStudentID;         /* ID of the student */
} WaitlistEntry;
typedef struct PQNode {
WaitlistEntry info;     /* WaitlistEntry stored in the node (sorted with largest priority first) */
struct PQNode* pNext;   /* Pointer to next node in the LL */
struct PQNode* pPrev;   /* Pointer to previous node in the LL */
} PQNode;
typedef struct{
int iCourseNumber;      /* Course number of the course */
int* iStudentIDs;       /* Array of IDs of students enrolled in the course */
int iNumEnrolled;       /* Number of Students currently enrolled in course */
int iMaxEnrolled;       /* Max number of Students that can enroll in the course */
PQNode* pFront;         /* Priority queue representing the waitlist for the course */
} Course;

你很困惑,需要读一本关于C编程的书。你的麻烦首先是正确和一致地声明你的类型和结构。我建议:

typedef struct WaitlistEntry_st {
int iPriority;          /* Priority of the student to be enrolled */
int iStudentID;         /* ID of the student */
} WaitlistEntry;
typedef struct PQNode_st {
WaitlistEntry info;     /* WaitlistEntry stored in the node
(sorted with largest priority first) */
struct PQNode_st* pNext;   /* Pointer to next node in the LL */
struct PQNode_st* pPrev;   /* Pointer to previous node in the LL */
} PQNode;
typedef struct Course_st {
int iCourseNumber;      /* Course number of the course */
int* iStudentIDs;       /* Array of IDs of students enrolled in the course */
int iNumEnrolled;       /* Number of Students currently enrolled in course */
int iMaxEnrolled;       /* Max number of Students that can enroll
in the course */
PQNode* pFront;         /* Priority queue representing the waitlist 
for the course */
} Course;

请注意上面的一致性:structs(紧随struct后面的名称(的标签后缀为_st(在 C 中 - 但不在 C++ 中 -struct标签的名称与typedef的名称位于不同的命名空间中(。

然后你会编码

WaitlistEntry* w1 = malloc(sizeof(WaitlistEntry));
if (!w1) { perror("malloc WaitlistEntry"); exit(EXIT_FAILURE); };
w1->iStudentID = iStudentID;
w1->iPriority = iPriority;

请注意,对malloc的每个调用都应针对失败进行测试。

顺便说一句,我建议阅读一些现有的代码以获得灵感。你会发现很多自由软件代码(例如在github或sourceforge上(,阅读其中一些是鼓舞人心和有启发性的。

不要忘记启用所有警告和调试信息。因此,-Wall -Wextra -g传递给开发C++ IDE 使用的gcc编译器(可能是 MINGW 变体(。

任一更改:

typedef struct {
// ...
} WaitlistEntry;

typedef struct WaitlistEntry {
// ...
} WaitlistEntry;

如果您选择这样做,我会使用这种样式:(减去评论(

// typedef's "struct WaitlistEntry" as "WaitlistEntry"
typedef struct WaitlistEntry WaitlistEntry;
// declares the contents of "struct WaitlistEntry"
struct WaitlistEntry {
// ...
};

这使得typedef的含义更加明显。混合 typedef 和结构声明可能会有点混乱。(它还允许您在WaitlistEntry中使用WaitlistEntry *

或者,您可以将malloc呼叫更改为:

w1 = (WaitlistEntry *) malloc(sizeof(WaitlistEntry));

你应该尽量与你的用法保持一致:(如果你使用WaitlistEntry总是使用WaitlistEntry,如果你使用struct WaitlistEntry总是使用struct WaitlistEntry(。

相关内容

  • 没有找到相关文章

最新更新