我可以使用数组中的字符串使用串联,宏或类似的字符串来命名结构变量?

发布于 2025-02-11 07:49:37 字数 770 浏览 1 评论 0原文

我有一系列动物名称,即我打算创建每个结构“动物”并将其存储在动物结构的farm_animals中。

typedef struct ani Animal;

动物* farm_animals [128] = {0};

尽管以下代码完全无效,但我在此包含它以显示我所想的准确的内容实践实现。我想声明类型的动物变量,该变量与数组中的字符串相对应,然后以某种方式将其用作动物的名称并将其存储在阵列中。

char* animal_names [] = {"Oliver", "Marcus", "Mike", "John", "Tom", "Daisy", "Lilac", "Rose", "Jim"};

    for (int i = 0; i < 9; i++) { 
        animal animal_names[i];
        farm_animals[i] = animal_names[i];
    }

我研究并发现了许多其他类似的帖子,这些帖子得出结论,由于C是一种未经解释的语言,因此无法用字符串值命名变量。但是,我想知道是否可以将字符串名称与后缀(例如索引号)连接起来,以创建一个全新的“字符串名称”来引用该动物。我也使用数组或相同的andial_names数组的宏,但这尚不清楚我可以作为初学者实现。

我认为C中的这种想法是牵强的,但是我真的想知道是否有一种方法可以使用for for loop和in and name nands命名这些结构,而不是手动创建100多个结构。

I have an array of animal names in the order that I intend to create each struct 'animal' and store it in farm_animals, a struct of animals.

typedef struct ani animal;

animal* farm_animals[128] = {0};

Although the below code is totally invalid, I have included it here to show exactly what I have thought of achieving in practice. I want to declare a variable of type animal corresponding to a string literal in the array, and then somehow use that literal as the name of the animal and store it in an array.

char* animal_names [] = {"Oliver", "Marcus", "Mike", "John", "Tom", "Daisy", "Lilac", "Rose", "Jim"};

    for (int i = 0; i < 9; i++) { 
        animal animal_names[i];
        farm_animals[i] = animal_names[i];
    }

I have researched and found many other similar posts which conclude that since C is a compiled not interpreted language, it is not possible to name a variable with the value of a string. However, I was wondering if it is possible to concatenate the string name with a suffix (like the index number) to create an entirely new 'string name' to refer to the animal. I have also though of a macro using an array or the same animal_names array, but this has not been clear for me to implement as a beginner.

I think this sort of idea in C is far-fetched, but I really wonder if there is a way to name these structs using a for loop and array of names, rather than manually creating 100+ structs.

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

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

发布评论

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

评论(1

如日中天 2025-02-18 07:49:37

是的,不可能在C中动态命名变量。如果我理解正确,您想创建与字符串相对应的动物类型,然后将其命名为该名称。

使用 structs 是您可以将相关/相关数据组合到单个中,变量(结构) - 本身是多个变量的集合。

我建议组织这样的内容:

typedef struct animal {
  char[20] name;
  char[20] type;
} animal;

您也可以用指向字符串的指针替换字符阵列(char*)。另外,如果您知道可以创建的动物类型/a>这样:

#define MAX_NAME_LEN 20
#define MAX_NUM_ANIMALS 10

enum animalType {
  LION,
  ELEPHANT,
  FALCON,
  PARROT
};

typedef struct animal {
  char[MAX_NAME_LEN] name;
  enum animalType type;
} animal;

int main(void) {
  animal listOfAnimals[MAX_NUM_ANIMALS];

  listOfAnimals[0].type = PARROT;
  listOfAnimals[0].name = "my parrot";

  return 0;
}

因此,您将制作一系列结构并将动物类型存储为数据成员,而不是制作100多个结构。然后,您可以根据需要使用一些映射机制或条件逻辑将这些类型分配给结构变量。

Yes, it is not possible to dynamically name variables in C. If I understand correctly, you want to create an animal type corresponding to a string and then name it that name.

The advantage with using structs is that you can combine related/relevant data into a single variable (the struct) – which itself is a collection of multiple variables.

I would suggest organizing something like this:

typedef struct animal {
  char[20] name;
  char[20] type;
} animal;

You can replace the character array with a pointer to the string (char*) as well. Also, if you know the type of animals that could be created you can instead create an enum like this:

#define MAX_NAME_LEN 20
#define MAX_NUM_ANIMALS 10

enum animalType {
  LION,
  ELEPHANT,
  FALCON,
  PARROT
};

typedef struct animal {
  char[MAX_NAME_LEN] name;
  enum animalType type;
} animal;

int main(void) {
  animal listOfAnimals[MAX_NUM_ANIMALS];

  listOfAnimals[0].type = PARROT;
  listOfAnimals[0].name = "my parrot";

  return 0;
}

So, instead of making 100+ structs, you would make an array of structs and store the type of animal as a data member. Then you could use some mapping mechanism or conditional logic to assign these types to the struct variables as per your needs.

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