将数据存储在头文件中包含数组的 Stucts 中
Storing Data in Stucts Containing Arrays in a Header File
我目前正在尝试将从函数输入的信息存储到我的头文件中声明的结构中,并在主文件中使用它。我不能使用结构数组,因为我不允许分配内存。
头文件
#ifndef HOMEWORK_H_
#define HOMEWORK_H_
typedef struct
{
int CourseID[25];
char CourseName[100][25];
}Course;
void NewCourse(void);
#endif
我的代码
#include <stdio.h>
#include <stdlib.h>
#include "Homework.h"
void NewCourse()
{
int i;
int CNumber = 0;
Course storeC;
for(i = 0; i < 0; i++)
{
if(storeC.CourseID[i] == 0)
{
if(storeC.CourseName[i] == NULL)
{
int CNumber = i;
break;
}
}
}
printf("%d\n", CNumber);
printf("Please enter the course's ID number: ");
scanf("%d", &storeC.CourseID[CNumber]);
printf("Please enter the course's name: ");
scanf("%s", storeC.CourseName[CNumber]);
}
我的主要内容并不适用,因为问题在于存储数据。
有几件事要记住,我必须为我的函数使用一个单独的文件,我必须为我的结构使用一个头文件。
我知道我的 for 循环确定数组中的位置可能无效,但我现在并不担心。
My question is how do I store the data from this function to the
header file?
更新
我更改了 main 函数以适应其他所有功能,但我现在遇到了这个错误。
a label can only be part of a statement and a declaration is not a
statement
main中的代码是:
switch(Option)
{
case 1:
Course c = NewCourse();
printf("%d\n%s\n", c.CourseID[0], c.CourseName[0]); // For testing purposes
break;
导致错误的原因是什么,因为它说它源于第 29 行,即 Course c = NewCourse();
?
将NewCourse
改为return一个Course
.
Course NewCourse(void);
将实现更改为:
Course NewCourse()
{
int i;
int CNumber = 0;
Course storeC;
...
return storeC;
}
相应地更改 main
。
int main()
{
Course c = NewCourse();
}
PS
你说,
I cannot use struct arrays because I am not allowed to allocate memory.
我假设这意味着您不能使用动态内存分配。如果允许在堆栈中创建 struct
数组,则可以使用以下方法简化代码:
typedef struct
{
int CourseID[25];
char CourseName[100];
}Course;
void NewCourse(Course course[]);
并在 main
中使用:
Course courses[25];
NewCourse(courses)
响应您的更新
您需要在代码周围添加一个范围块 { }
,如下所示:
int main()
{
{
Course c = NewCourse();
}
}
这应该可以解决您的错误并允许您的代码编译。
此外,您在操作 CNumber 变量时出错。它被声明了两次,具有不同的范围:
int CNumber = 0;
// NewCourse 函数范围内的第一个定义
然后在测试中,使用块作用域:
if(storeC.CourseID[i] == 0)
{
if(storeC.CourseName[i] == NULL)
{
int CNumber = i; // block-scope. This is not the same CNumber Variable (todo: Omit int)
break;
}
}
因此,当您稍后在
中引用它时
printf("%d\n", CNumber);
printf("Please enter the course's ID number: ");
scanf("%d", &storeC.CourseID[CNumber]);
printf("Please enter the course's name: ");
scanf("%s", storeC.CourseName[CNumber]);
它将始终引用函数范围变量,该变量始终为零。
解决方法:省略测试中的 int 声明:
if(storeC.CourseName[i] == NULL)
{
CNumber = i;
break;
}
我目前正在尝试将从函数输入的信息存储到我的头文件中声明的结构中,并在主文件中使用它。我不能使用结构数组,因为我不允许分配内存。
头文件
#ifndef HOMEWORK_H_
#define HOMEWORK_H_
typedef struct
{
int CourseID[25];
char CourseName[100][25];
}Course;
void NewCourse(void);
#endif
我的代码
#include <stdio.h>
#include <stdlib.h>
#include "Homework.h"
void NewCourse()
{
int i;
int CNumber = 0;
Course storeC;
for(i = 0; i < 0; i++)
{
if(storeC.CourseID[i] == 0)
{
if(storeC.CourseName[i] == NULL)
{
int CNumber = i;
break;
}
}
}
printf("%d\n", CNumber);
printf("Please enter the course's ID number: ");
scanf("%d", &storeC.CourseID[CNumber]);
printf("Please enter the course's name: ");
scanf("%s", storeC.CourseName[CNumber]);
}
我的主要内容并不适用,因为问题在于存储数据。
有几件事要记住,我必须为我的函数使用一个单独的文件,我必须为我的结构使用一个头文件。
我知道我的 for 循环确定数组中的位置可能无效,但我现在并不担心。
My question is how do I store the data from this function to the header file?
更新
我更改了 main 函数以适应其他所有功能,但我现在遇到了这个错误。
a label can only be part of a statement and a declaration is not a statement
main中的代码是:
switch(Option)
{
case 1:
Course c = NewCourse();
printf("%d\n%s\n", c.CourseID[0], c.CourseName[0]); // For testing purposes
break;
导致错误的原因是什么,因为它说它源于第 29 行,即 Course c = NewCourse();
?
将
NewCourse
改为return一个Course
.Course NewCourse(void);
将实现更改为:
Course NewCourse() { int i; int CNumber = 0; Course storeC; ... return storeC; }
相应地更改
main
。int main() { Course c = NewCourse(); }
PS
你说,
I cannot use struct arrays because I am not allowed to allocate memory.
我假设这意味着您不能使用动态内存分配。如果允许在堆栈中创建 struct
数组,则可以使用以下方法简化代码:
typedef struct
{
int CourseID[25];
char CourseName[100];
}Course;
void NewCourse(Course course[]);
并在 main
中使用:
Course courses[25];
NewCourse(courses)
响应您的更新
您需要在代码周围添加一个范围块 { }
,如下所示:
int main()
{
{
Course c = NewCourse();
}
}
这应该可以解决您的错误并允许您的代码编译。
此外,您在操作 CNumber 变量时出错。它被声明了两次,具有不同的范围:
int CNumber = 0;
// NewCourse 函数范围内的第一个定义
然后在测试中,使用块作用域:
if(storeC.CourseID[i] == 0)
{
if(storeC.CourseName[i] == NULL)
{
int CNumber = i; // block-scope. This is not the same CNumber Variable (todo: Omit int)
break;
}
}
因此,当您稍后在
中引用它时printf("%d\n", CNumber);
printf("Please enter the course's ID number: ");
scanf("%d", &storeC.CourseID[CNumber]);
printf("Please enter the course's name: ");
scanf("%s", storeC.CourseName[CNumber]);
它将始终引用函数范围变量,该变量始终为零。
解决方法:省略测试中的 int 声明:
if(storeC.CourseName[i] == NULL)
{
CNumber = i;
break;
}