在这种情况下我使用realloc时遇到了麻烦。我的编译器devC ++一直告诉我,对于不完整类型'struct WaitlistEntry','sizeof'是无效的应用程序。我基本上尝试使用给定的结构来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;
答案 0 :(得分:1)
你很困惑,需要阅读一本关于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;
请注意上面的一致性:struct
s的标记(紧随struct
之后的名称)后缀为_st
(在C中 - 但不在C ++中 - {{名称} 1}}标签与struct
)的名称位于不同的名称空间中。
然后你会编码
typedef
请注意,对 WaitlistEntry* w1 = malloc(sizeof(WaitlistEntry));
if (!w1) { perror("malloc WaitlistEntry"); exit(EXIT_FAILURE); };
w1->iStudentID = iStudentID;
w1->iPriority = iPriority;
的每次调用都应该针对失败进行测试。
顺便说一句,我建议阅读一些现有的代码以获取灵感。你会发现很多free software代码(例如github或sourceforge)并且阅读其中一些代码是鼓舞人心和有启发性的。
不要忘记启用所有警告和调试信息。因此,将malloc
传递给Dev-C ++ IDE使用的-Wall -Wextra -g
编译器(可能是MINGW变体)。
答案 1 :(得分:1)
要么改变:
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和struct声明可能有点令人困惑。 (它还允许您在WaitlistEntry *
内使用WaitlistEntry
。)
或者您可以将malloc
调用更改为:
w1 = (WaitlistEntry *) malloc(sizeof(WaitlistEntry));
您应该尝试与您的使用保持一致:(如果您使用WaitlistEntry
,请始终使用WaitlistEntry
,如果您使用struct WaitlistEntry
,请始终使用struct WaitlistEntry
。