c - 使用结构的年度学生计划
问题描述
我的第一个问题是我无法制作 for 循环并将其实现到代码中并以某种方式完成我的程序。
我的第二个问题是编译器显示问题:memset(database,0,SIZE*sizeof(struct student));
缺少类型说明符,默认为“int”
在程序结束时,我对memcpy(database.name,name,size_of_name);
成员引用基类型“struct student [100]”有问题不是结构或联合
有我的结构和功能:
#define SIZE 100
struct student {
char name[SIZE];
int votes;
};
struct student database[SIZE];
memset(database,0,SIZE*sizeof(struct student));
int size = 0;
int find_student(struct student* students,int size, const char* name){
// for loop,which take all entries in database
// if it find same name, then reutrn his index
//otherwise reuturn -1;
int i;
for(i=0; i<SIZE;i++){
// how to make that loop
}
return -1;
}
int compare(const void* p1, const void* p2){
struct student* s1 = (struct student*)p1;
struct student* s2 = (struct student*)p2;
// s1->votes
// s1->name
return 0;
}
还有我已经做过的代码(对不起我的语法):
char line[SIZE];
memset(line,0,SIZE);
char* r = fgets(line,SIZE,stdin);
if (r == NULL){
printf("End of input");
return (-1);
}
char* end = NULL;
int value = strtol(line,&end,10);
if (value == 0){
printf("Convertion was not sucessful");
return (-1);
}
// helping array
char name[SIZE];
// set on zero
memset(name,0,SIZE);
// get begining of a name = one position after space
char* beginning_name = end + 1;
// Size of name is number of signs to end of string
// minus end of line
int size_of_name = strlen(beginning_name) - 1;
if (size_of_name > 0){
// copy
memcpy(name,beginning_name,size_of_name);
// At the end is saved string with name
// without end of line and with zero at the end
}
else {
// failed to read a name
printf("Failed to read a name");
return (-1);
}
int id = find_student(database,size,name);
if (id< 0){
// copy it to last place in array
memcpy(database.name,name,size_of_name);
// increase number of entries
size+=1;
}
else {
// there I need add to votes,something like votes++;
}
}
抱歉格式化,但我是stackoverflow的新手。
解决方案
推荐阅读
- python - 机器学习中的步长和学习率有什么区别?
- r - 如何在 R 中使用 Inf/-Inf 预测数据?
- javascript - 如何在 2 列中制作手风琴(左侧内容链接到右侧)
- guidewire - 如何在 .pcf 文件中编写代码部分或方法?
- javascript - 未捕获的 SyntaxError:输入意外结束 - sid
- visual-studio - 有没有类似于 MSVS 的“#pragma GCC system_header”的东西?
- javascript - 加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应
- c# - ASP.NET Core 应用程序不会在容器中启动
- windows - 如何在 Windows 中为某些目录使用替代名称?
- python - 对象在我的 pygame 代码中只移动一种方式,我该如何解决这个问题?