英文:
How to Initialize a database from a header file or c type file
问题
我正在制作一个使用C语言创建“猫”数据库的作业,使用结构体记录猫的名字、性别、重量等信息。我已经创建了以下几个头文件:
Cat.h
#pragma once
#include <stdint.h>
const int MAX_CAT_NAME = 50;
enum Gender {
MALE,
FEMALE,
UNKNOWN
};
struct Cat{
char name[MAX_CAT_NAME + 1];
Gender gender = UNKNOWN;
float weightInPounds = 0.01;
uint32_t chipId = 0;
bool isFixed = false;
};
ArrayDB.h
#pragma once
#include "Cat.h"
#define MAX_CATS 100
extern Cat catArray[MAX_CATS];
extern int* numCats;
ArrayDB.c
#include "ArrayDB.h"
#include "Cat.h"
Cat catArray[MAX_CATS];
void initDB() {
*numCats = 0;
for (int i = 0; i < MAX_CATS; i++) {
Cat newCat = generateCat(); // You need to define generateCat function
if (validateCat(&newCat)) { // You need to define validateCat function
catArray[*numCats] = newCat;
(*numCats)++;
}
}
}
在你的ArrayDB.c
中,你需要确保已经实现了generateCat
和validateCat
这两个函数,因为你尝试在initDB
中调用它们。这些函数的具体实现应该在Cat.c
文件中。
请注意,你的代码还需要包含Cat.c
文件的内容,因为它包含了用于生成猫的名称、验证猫信息等的函数。如果你在Cat.c
中定义了这些函数,你的代码应该能够成功初始化猫数据库。
希望这可以帮助你初始化数据库。如果你有进一步的问题或需要更多的帮助,请随时提出。
英文:
I'm working on an assignment of creating a "cat" database using a struct to record the name, gender, weight etc. of cats using C. I have made a few header files below:
Cat.h
#pragma once
#include <stdint.h>
const int MAX_CAT_NAME = 50;
enum Gender {
MALE,
FEMALE,
UNKNOWN
};
struct Cat{
char name[MAX_CAT_NAME + 1];
Gender gender = UNKNOWN;
float weightInPounds = 0.01;
uint32_t chipId = 0;
bool isFixed = false;
};
ArrayDB.h
#pragma once
#include "Cat.h"
#define MAX_CATS 100
extern Cat catArray[ MAX_CATS ];
extern int* numCats;
and ArrayDB.c
#include "ArrayDB.h"
#include "Cat.h"
Cat catArray[ MAX_CATS ];
void initDB() {
*numCats = 0;
for( int i = 0; i < MAX_CATS; i++ ) {
Cat Cat = generateCat();
if( validateCat(&Cat) ) {
catArray[ numCats++ ] = Cat;
}
}
}
There is also a Cat.c
file I have linked with struct Cat to generate the cat's name, validate the cat and print the cat, but I'm not sure if it is needed to initialize the database
In my assignment instructions, I was told for the header file, ArrayDB.h
:
- Declare the max size of the cat array as MAX_CATS in
ArrayDB.h
- Declare a cat array in
ArrayDB.h
- Declare the current array size as numCats in
ArrayDB.h
I've done this, but I'm stuck on the next step for ArrayDB.c:
"Define all three declarations in ArrayDB.c
(I hope I've done this correctly) and write a function called initDB in ArrayDB.c to initialize the database." The return type and parameters were left up to me, so I just used a void function.
In my code for ArrayDB.c
, I've attempted to call the functions that I've written from Cat.c
(That also uses variables from struct Cat) to initialize the "cat" database, but I have compilations errors in ArrayDB.c
that say generateCat and validateCat() are undeclared. I thought if I included the header file from Cat.h
, I'd be able to initialize the database, but I think this isn't how I can do this.
Can someone show me how I can initialize the database in my function void initDB()?
I thought I can do it with my header files, but I'm not sure now.
I can paste my Cat.c
code if it's needed. I would appreciate any guidance.
答案1
得分: 1
在C语言中,你初始化变量(类型的实例),而不是类型struct Cat
,这是语法错误。你还应该使用符号常量(#define),而不是const变量
。你缺少Gender
和Cat
的typedef,以便能够像你所做的那样使用它们。
我建议你避免使用全局变量,而是在main()
中定义你的数据库:
如果你生成struct Cat
的实例,就不需要验证它们。
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_CATS 100
#define MAX_CAT_NAME 50
enum Gender {
MALE,
FEMALE,
UNKNOWN
};
struct Cat {
char name[MAX_CAT_NAME + 1];
enum Gender gender;
float weightInPounds;
uint32_t chipId;
bool isFixed;
};
void initDB(struct Cat cats[MAX_CATS]) {
for(size_t i = 0; i < MAX_CATS; i++ )
cats[i] = (struct Cat) { "", UNKNOWN, 0.01, 0, 0 };
}
int main() {
struct Cat cats[MAX_CATS];
initDB(cats);
}
英文:
In C you initialize variables (instance of a type) not the type struct Cat
which is syntax error. You also want to use a symbolic constants (#define) instead of a const variable
. You are missing typedefs for Gender
and Cat
to be able to use them the way you do.
I suggest you avoid using global variables so you define your database in main() instead:
If you generate an instance of struct Cat
there should be no need to validate them.
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_CATS 100
#define MAX_CAT_NAME 50
enum Gender {
MALE,
FEMALE,
UNKNOWN
};
struct Cat {
char name[MAX_CAT_NAME + 1];
enum Gender gender;
float weightInPounds;
uint32_t chipId;
bool isFixed;
};
void initDB(struct Cat cats[MAX_CATS]) {
for(size_t i = 0; i < MAX_CATS; i++ )
cats[i] = (struct Cat) { "", UNKNOWN, 0.01, 0, 0 };
}
int main() {
struct Cat cats[MAX_CATS];
initDB(cats);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论