如何从头文件或C类型文件初始化数据库

huangapple go评论62阅读模式
英文:

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中,你需要确保已经实现了generateCatvalidateCat这两个函数,因为你尝试在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 &lt;stdint.h&gt;

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 &quot;Cat.h&quot;

#define MAX_CATS 100

extern Cat catArray[ MAX_CATS ];

extern int* numCats;

and ArrayDB.c

#include &quot;ArrayDB.h&quot;
#include &quot;Cat.h&quot;

Cat catArray[ MAX_CATS ];

void initDB() {
    *numCats = 0;

    for( int i = 0; i &lt; MAX_CATS; i++ ) {
        Cat Cat = generateCat();
        if( validateCat(&amp;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变量。你缺少GenderCat的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 &lt;stdint.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdbool.h&gt;

#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 &lt; MAX_CATS; i++ )
        cats[i] = (struct Cat) { &quot;&quot;, UNKNOWN, 0.01, 0, 0 };
}

int main() {
	struct Cat cats[MAX_CATS];
	initDB(cats);
}

huangapple
  • 本文由 发表于 2023年2月26日 20:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定