如何在另一个结构中使用一个结构中的元素?

发布于 2025-01-12 02:32:25 字数 1162 浏览 3 评论 0原文

作为参考,我在工作中跟随导师学习 C,提供的代码和文本来自 Zed Shaw 的“Learn C the Hard Way”,第 17 章,额外学分。

工作代码中的原始代码片段是:

#define MAX_DATA 512
#define MAX_ROWS 100


struct Address {
    int id;
    int set;
    char name[MAX_DATA];
    char email[MAX_DATA];
};

struct Database {
    struct Address rows[MAX_ROWS];
};

额外的信用请求是

更改代码以接受 MAX_DATA 和 MAX_ROWS 参数,将它们存储在 数据库结构体,并将其写入文件,从而创建一个可以任意修改的数据库 大小。

我这样编写代码:

//#define MAX_DATA 
//#define MAX_ROWS 

struct Address {
    int id;
    int set;
    
    struct Database *db;
    char name[db->MAX_DATA];
    char email[db->MAX_DATA];

};

struct Database {
    int MAX_DATA;
    int MAX_ROWS;
    struct Address rows[MAX_ROWS];
};

编译时,我收到这些错误。

    >  error: ‘db’ undeclared here (not in a function)    
       char name[db->MAX_DATA];
                 ^~ 
       error: ‘MAX_ROWS’ undeclared here (not in a function)
         struct Address rows[MAX_ROWS];
                             ^~~~~~~~

我对这个问题/任务有点困惑,所以希望有人能提供一些好的意见。另外,如果我在任何术语上有误,请告诉我,因为我没有运气使用上面使用的术语谷歌搜索这个问题。

谢谢。

For reference, I'm following a mentorship to learn C at my work and the provided code and text is from Zed Shaw's "Learn C the Hard Way", Chapter 17, Extra Credit.

the original code snippet from the working code is:

#define MAX_DATA 512
#define MAX_ROWS 100


struct Address {
    int id;
    int set;
    char name[MAX_DATA];
    char email[MAX_DATA];
};

struct Database {
    struct Address rows[MAX_ROWS];
};

The extra credit request is

Change the code to accept parameters for MAX_DATA and MAX_ROWS, store them in the
Database struct, and write that to the file, thus creating a database that can be arbitrarily
sized.

I wrote my code as such:

//#define MAX_DATA 
//#define MAX_ROWS 

struct Address {
    int id;
    int set;
    
    struct Database *db;
    char name[db->MAX_DATA];
    char email[db->MAX_DATA];

};

struct Database {
    int MAX_DATA;
    int MAX_ROWS;
    struct Address rows[MAX_ROWS];
};

When compiling, I get these errors.

    >  error: ‘db’ undeclared here (not in a function)    
       char name[db->MAX_DATA];
                 ^~ 
       error: ‘MAX_ROWS’ undeclared here (not in a function)
         struct Address rows[MAX_ROWS];
                             ^~~~~~~~

I'm a bit stumped on this issue/task so hoping someone has some good input. Also please let me know if I am wrong on any terms, as I have not had luck with googling this issue using the terms I have used above.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2025-01-19 02:32:25

在您的代码中,您正在定义稍后使用的结构,但尚未创建它们的任何实例。所以你不能引用db->MAX_DATA,因为db还不存在,它只是一个未初始化的指针。

定义静态数组时引用的每个变量都必须是常量(编译器需要在运行之前知道编译时的最终值)。当您无法使用常量时,因为在代码运行之前您不会知道其值,则需要使用动态内存。

struct Address {
  char* name;   // declared as pointers because
  char* email;  // they will be dynamically allocated 
};

struct Database {
  int max_data;
  int max_rows;
  struct Address* rows; // declared as another pointer
};

int main() {
  struct Database db;
  // You have to ask for 'max_data' and 'max_rows' normally
  // ...
  // When you know the value of max_rows, you can allocate memory for 'rows':
  db.rows = (struct Address*)malloc(sizeof(struct Address) * db.max_rows);

  for(int i = 0; i < db.max_rows; ++i) {
    // You now have a dynamic array of Address but you still need to allocate memory for 'name' and 'email':
    db.rows[i].name = (char*)malloc(sizeof(char) * db.max_data);
    db.rows[i].email = (char*)malloc(sizeof(char) * db.max_data);
    // Here you can ask for 'name' and 'email' and store them in db.rows[i] -> TIP: Use fgets()
  }
}

PD:不要忘记以正确的顺序释放动态分配的内存。首先,您必须释放内部存储器(名称电子邮件),然后释放外部存储器(

In your code, you are defining structs to use later but you are not creating any instances of them yet. So you cannot refer to db->MAX_DATA because db does not exist yet, it is just an uninitialized pointer.

Every variable you refer to when you define static arrays must be a constant (the compiler needs to know the final value at compile time, before it is running). When you cannot use constants because you won't know the value until the code is running, you need to use dynamic memory.

struct Address {
  char* name;   // declared as pointers because
  char* email;  // they will be dynamically allocated 
};

struct Database {
  int max_data;
  int max_rows;
  struct Address* rows; // declared as another pointer
};

int main() {
  struct Database db;
  // You have to ask for 'max_data' and 'max_rows' normally
  // ...
  // When you know the value of max_rows, you can allocate memory for 'rows':
  db.rows = (struct Address*)malloc(sizeof(struct Address) * db.max_rows);

  for(int i = 0; i < db.max_rows; ++i) {
    // You now have a dynamic array of Address but you still need to allocate memory for 'name' and 'email':
    db.rows[i].name = (char*)malloc(sizeof(char) * db.max_data);
    db.rows[i].email = (char*)malloc(sizeof(char) * db.max_data);
    // Here you can ask for 'name' and 'email' and store them in db.rows[i] -> TIP: Use fgets()
  }
}

PD: Don't forget to free the dynamically allocated memory in the correct order. First you have to free the internal memory (name and email) and then the external memory (rows)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文