【发布时间】:2015-12-17 00:30:02
【问题描述】:
我目前正在尝试将从函数输入的信息存储到我的头文件中声明的结构中,并在主文件中使用它。我不能使用结构数组,因为我不允许分配内存。
头文件
#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]);
}
我的 main 并不真正适用,因为问题在于存储数据。
需要记住的一些事情是我必须为我的函数使用一个单独的文件,并且我必须为我的结构使用一个头文件。
我知道我的 for 循环来确定数组中的哪个位置可能无效,但我现在并不那么担心。
我的问题是如何将数据从这个函数存储到 头文件?
更新
我更改了 main 函数以适应其他所有内容,但现在我遇到了这个错误。
标签只能是语句的一部分,而声明不是 声明
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();?
【问题讨论】:
-
"不允许分配内存"。你意识到堆栈是一个被分配的内存区域,对吧?
-
我的意思是使用 Malloc 或 Calloc
-
那你的意思是你不允许动态分配任何内存。
标签: c arrays function struct header-files