C-如何将realloc与指针一起使用
在这种情况下,我在使用realloc时遇到问题。我的编译器devC++一直告诉我,对于不完整的类型“struct WaitlistEntry”,sizeof的应用程序无效。我基本上是尝试使用给定的结构malloc一个结构,然后将数据存储在其中 以下是整个功能:C-如何将realloc与指针一起使用,c,struct,malloc,C,Struct,Malloc,在这种情况下,我在使用realloc时遇到问题。我的编译器devC++一直告诉我,对于不完整的类型“struct WaitlistEntry”,sizeof的应用程序无效。我基本上是尝试使用给定的结构malloc一个结构,然后将数据存储在其中 以下是整个功能: void attemptEnrollment( Course* pCourse, int iStudentID, int iPriority ){ if (pCourse->iNumEnrolled < pCour
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);
}
}
你很困惑,需要读一本关于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;
注意上面的一致性:结构的标签,紧接着结构的名称是用Syt和c在后缀中,而不是在C++中,结构标签的名称与TyPulf的名称不同。 然后你就可以编码了
WaitlistEntry* w1 = malloc(sizeof(WaitlistEntry));
if (!w1) { perror("malloc WaitlistEntry"); exit(EXIT_FAILURE); };
w1->iStudentID = iStudentID;
w1->iPriority = iPriority;
请注意,对malloc的每个调用都应该针对失败进行测试
顺便说一句,我建议阅读一些现有的代码以获得灵感。你会发现很多代码,例如,在或上,阅读其中一些代码是鼓舞人心和有教育意义的
不要忘记启用所有警告和调试信息。因此,将-Wall-Wextra-g传递给您的gcc编译器,可能是Dev-C++IDE使用的MINGW变体。更改:
typedef struct {
// ...
} 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调用更改为:
您应该尝试与您的用法保持一致:如果您使用WaitlistEntry,请始终使用WaitlistEntry;如果您使用struct WaitlistEntry,请始终使用struct WaitlistEntry。您需要保持一致。第一行应该是typedef struct PWaitlistEntry,您应该使用struct PWaitlistEntry而不是struct WaitlistEntry您没有声明任何名为struct WaitlistEntry的内容。您分配了一个匿名结构,其typedef名称为WaitlistEntry。如果将声明更改为:typedef struct WaitlistEntry{int-iPriority;/*待注册学生的优先级*/int-iStudentID;/*学生的ID*/}WaitlistEntry;你应该更幸运你的麻烦不在realloc身上。首先要正确且一致地声明结构。顺便说一句,你的名字很差。我建议使用typedef结构WaitlistEntry{int-iPriority;int-iStudentID;}WaitlistEntry;在C中,而不是C++中,结构标签的名称和TyPulf的名称在不同的名称空间中,不是编译器,而是AN。在Windows上,您的编译器可能是MinGW,它是的某个变体。在Linux上可能是这样。顺便说一句,在MacOSX上,您的标题提到了realloc,您显示的代码甚至没有使用。。。。下一次,在发帖之前,多问一些问题,并把它读几遍。我应该提到的是,这些结构是我作业的一部分。我认为在某种程度上实施这些措施是有意的。我确实有这些怀疑,但我们得到了关于实际编写哪些部分的指示,而对于我发布的内容,这只是attemptEnrollment函数中的内容。不是返回类型、传递的参数等。也就是说,感谢您的输入。我不认为结构会造成这么大的问题。老师是人,他们会打字和犯错。但您确定在问题中正确复制了代码吗?这是如此糟糕的要求和制定,我会想象你不是你的教练作出的打字错误。是的,我没有触及这一部分。你可以查看原始的-用更好的格式编辑的页面。我做了,它一直工作到需要填充的部分。然后我猜你的老师希望你纠正打字错误。
typedef struct WaitlistEntry {
// ...
} WaitlistEntry;
// typedef's "struct WaitlistEntry" as "WaitlistEntry"
typedef struct WaitlistEntry WaitlistEntry;
// declares the contents of "struct WaitlistEntry"
struct WaitlistEntry {
// ...
};
w1 = (WaitlistEntry *) malloc(sizeof(WaitlistEntry));