Error handling 代码中的逻辑错误
我应该填写的代码很简单Error handling 代码中的逻辑错误,error-handling,Error Handling,我应该填写的代码很简单 #define MAX_NAME_LEN 128 typedef struct { char name[MAX_NAME_LEN]; unsigned long sid; } Student; /* return the name of student s */ const char* getName(const Student* s) { return s->name; } /* set the name of student s */ void setNa
#define MAX_NAME_LEN 128
typedef struct {
char name[MAX_NAME_LEN];
unsigned long sid;
} Student;
/* return the name of student s */
const char* getName(const Student* s) {
return s->name;
}
/* set the name of student s */
void setName(Student* s, const char* name) {
/* fill me in */
}/* return the SID of student s */
unsigned long getStudentID(const Student* s) {
/* fill me in */
}
/* set the SID of student s */
void setStudentID(Student* s, unsigned long sid) {
/* fill me in */
}
但是,它说明了以下函数中的逻辑错误是什么
Student* makeDefault(void) {
Student s;
setName(&s, "John");
setStudentID(&s, 12345678);
return &s;
}
我看不出有什么问题。我测试过了。它很好用
是因为这可能是一个void函数,不需要返回任何内容吗?您不能返回指向本地声明变量(Student s)的指针。返回后,变量“s”将消失(成为垃圾) 相反,您需要首先分配一名学生 您可能应该这样做:
void makeDefault(Student* pS) {
setName( pS, "John");
setStudentID( pS, 12345678);
}
然后让调用应用程序分配学生