【问题标题】:Storing Data in Stucts Containing Arrays in a Header File将数据存储在包含头文件中的数组的结构中
【发布时间】: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


【解决方案1】:
  1. 更改NewCourse 以返回Course

    Course NewCourse(void);
    
  2. 将实现更改为:

    Course NewCourse()
    {
       int i;
       int CNumber = 0;
    
       Course storeC;
    
       ...
    
       return storeC;
    }
    
  3. 相应地更改main

    int main()
    {
        Course c = NewCourse();
    }
    

附言

你说,

我不能使用结构体数组,因为我不能分配内存。

我认为这意味着您不能使用动态内存分配。如果允许您在堆栈中创建 structs 数组,则可以使用以下方法简化代码:

typedef struct
{
   int CourseID[25];
   char CourseName[100];
}Course;

void NewCourse(Course course[]);

main 中,使用:

Course courses[25];
NewCourse(courses)

响应您的更新

您需要在代码周围添加一个范围块{ },如下所示:

int main()
{
    {
       Course c = NewCourse();
    }
}

这应该可以解决您的错误并允许您的代码编译。

【讨论】:

  • 谢谢,我之前尝试过,但也无法更改 main。
  • 现在我唯一的问题是我在设置 Course c = NewCourse();
【解决方案2】:

此外,您在操作 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;
   }

【讨论】:

  • 非常感谢。老实说,我在 CNumber 之前没有注意到那个 int。我什至不知道为什么我把它包括在内,但谢谢你明白这一点。
  • 欢迎。很高兴它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多