迭代地为 C 结构赋值

发布于 2024-11-26 17:12:21 字数 457 浏览 3 评论 0原文

我有一个定义为的结构

typedef struct{
char string1
char string2
int number1
char string3
}structure1

,想要在这样的循环中将值分配给 string1,string2,number1,string3

structure1 bob
for(int i = 0,i<=4,i++)
{
bob.i = assigned value
}

现在我明白上面的通用形式的代码仅适用于整数,因为你不能只使用 string = string进行分配,但出现了同样的问题,因为我不知道如何引用结构内的值而不具体一一命名它们。对于字符串,将有第二个赋值,依赖于 i 索引来确定它当时是整数还是字符串,以便它可以执行赋值。我正在思考一些类似于枚举的东西,但我以前从未在实际意义上使用过它们,只是理论上的。

I have a structured defined as

typedef struct{
char string1
char string2
int number1
char string3
}structure1

and want to assign the values to string1,string2,number1,string3 in a loop like this

structure1 bob
for(int i = 0,i<=4,i++)
{
bob.i = assigned value
}

now I understand that code above in it's generic form will only work for integers as you can't just go string = string for assignment, but the same problem arises as I don't know how to reference the values inside a struct without specifically naming them one by one. for strings there will be a second assignment relying on the i index to work out if it's a integer or string at the time so it can perform the assignment. I was thinking something along the lines of enum's but I've never used them in a practical sense before, just theoretical.

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

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

发布评论

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

评论(4

短暂陪伴 2024-12-03 17:12:21

在 C 中这是不可能的。最接近的方法是计算字段偏移量,然后使用它们来分配值:

int fieldOffset[4];

structure1 base;

fieldOffset[0] = (char*)&base.string1 - (char*)&base;
fieldOffset[1] = (char*)&base.string2 - (char*)&base;
fieldOffset[2] = (char*)&base.number1 - (char*)&base;
fieldOffset[3] = (char*)&base.string3 - (char*)&base;

structure1 structYouWantToAssign;

for (int i = 0; i < 4; ++i)
{
    *((char*)&structYouWantToAssign + fieldOffset[i]) = assignedValue;
}

警告:此代码只是为了演示可以在没有名称的情况下分配字段,但是您不应该使用它!

It's not possible in C. The closest to that would be calculating field offsets and then using them to assign values:

int fieldOffset[4];

structure1 base;

fieldOffset[0] = (char*)&base.string1 - (char*)&base;
fieldOffset[1] = (char*)&base.string2 - (char*)&base;
fieldOffset[2] = (char*)&base.number1 - (char*)&base;
fieldOffset[3] = (char*)&base.string3 - (char*)&base;

structure1 structYouWantToAssign;

for (int i = 0; i < 4; ++i)
{
    *((char*)&structYouWantToAssign + fieldOffset[i]) = assignedValue;
}

Warning: this code is just to demonstrate it is possible to assign fields without their name, but you should not use it!

风吹雪碎 2024-12-03 17:12:21

不幸的是,这在 C 中是不可能的。这确实很酷,但语法中没有这样的内容。

Unfortunately that's not possible in C. It's be really cool, but nothing of the sort is in the syntax.

锦爱 2024-12-03 17:12:21

您可以尝试稍微不同的方法。想要这样做意味着您正在初始化它们,在这种情况下您可以这样做:(

#define STRUCT_DEF {"default", "another", 1234, "the last string" }
...
structure1 Variable = {"default", "another", 1234, "the last string"};

假设您的意思是 char string1[STR_LEN] 而不仅仅是单个字符。)

或者您可以尝试使用内部数组重新定义结构:

typedef struct{
char string[NUM_STRINGS][STR_LENGTH];
int number1;
}structure1

这样您可以在循环中寻址字符串,而无需单独命名每个字符串。

You could try a slightly different approach. Wanting to do that implies you're initialising them in which case you could do this:

#define STRUCT_DEF {"default", "another", 1234, "the last string" }
...
structure1 Variable = {"default", "another", 1234, "the last string"};

(that's assuming you meant char string1[STR_LEN] rather than just a single char.)

Alternatively you could try redefining your structure with arrays inside:

typedef struct{
char string[NUM_STRINGS][STR_LENGTH];
int number1;
}structure1

That way you can address the strings in a loop without naming each individually.

感情旳空白 2024-12-03 17:12:21

使用分配给成员的函数:-

void BobAssign(struct* s, int i, void * value);

Use a function which assigns to the member:-

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