需要改变不同结构中常见字段的方法 [英] Need way to alter common fields in different structs

查看:84
本文介绍了需要改变不同结构中常见字段的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里用C编程,用于Windows和各种Unix平台。我有一套有共同领域的结构,但也有不同的领域。例如:

  typedef struct {
char street [10];
char city [10];
char lat [10];
char long [10];
} ADDR_A;

typedef struct {
char street [10];
char city [10];
char zip [10];
char国家[10];
} ADDR_B;

他们显然并不那么简单,我实际上有6个不同的结构,但这是基本思想。

我想要的是一个函数,我可以将指针传递给其中的一个,并且能够更新公共字段。例如:

  static void updateFields(void * myStruct){
strcpy(myStruct-> street,streetFromSomethingElse) ;
strcpy(myStruct-> city,cityFromSomethingElse);

$ / code>

显然,由于void指针和某种类型的演员是有序的,但是没有什么好的方法来演绎那个(在那里?)。我不反对一个额外的参数,它以某种方式指定了结构的类型。



由于不相关的原因,我不能改变这些结构或将它们结合起来或对他们做任何事情。我必须照原样使用它们。



与往常一样,在此先感谢您的帮助。



编辑:正如我在下面的评论中所添加的,常见字段不能保证全部在开头或全部在结尾或其他任何地方。它们只是保证存在。

解决方案

考虑到结构体至少没有类似的布局,要做到这一点,既不短也不优雅:


  1. 使用散列表而不是结构来存储数据即可。这样做可以使用名称作为哈希表中的键来访问字段,而不管实际类型如何。这在C语言中需要很多麻烦,并且只有当你需要访问比如说一百个字段时,我才不会推荐它。

  2. 坚持强大的打字和清晰地更新字段。是的,这是你不想要的,但是对于适量的字段,这可能是好的。
  3. / strong>即可。不过,我认为尽可能避免使用宏是最好的选择。这是不值得的。


在C(是没有鸭子打字)。这是一个典型的例子,动态语言会更舒适(也许你可以从你的代码中调用python?:))

I'm programming in C here, for Windows and various Unix platforms. I have a set of structs that have common fields, but also fields that are different. For example:

typedef struct {
    char street[10];
    char city[10];
    char lat[10];
    char long[10];
} ADDR_A;

typedef struct {
    char street[10];
    char city[10];
    char zip[10];
    char country[10];
} ADDR_B;

They obviously aren't that simple, and I actually have 6 different structs, but that's the basic idea.

What I would like, is a function in which I could pass a pointer to one of these and be able to update the common fields. Something like:

static void updateFields( void *myStruct ) {
    strcpy( myStruct->street, streetFromSomethingElse );
    strcpy( myStruct->city, cityFromSomethingElse );
}

OBVIOUSLY, what I wrote there doesn't work due to the void pointer and a cast of some kind is in order, but there isn't really a good way to cast that (is there?). I'm not opposed to an additional argument that somehow specifies the type of the struct.

For reasons that aren't relevant, I CANNOT change these structs or combine them or do anything else with them. I HAVE to use them as they are.

As always, thanks in advance for the help.

EDIT: As I added in a comment below, the common fields are NOT guaranteed to be all at the beginning or all at the end or anything else. They are just guaranteed to exist.

解决方案

Considering that the structs don't have at least a similar layout, there are limited ways to accomplish this, neither are short or elegant:

  1. Use a hash table instead of structs to store your data. Doing so will make it possible to access fields with their name as the key in the hashtable, regardless of the actual type. This requires a lot of trouble in C, and only if you need to access say, a hundred fields, I wouldn't recommend it.

  2. Stick to strong typing and update the fields distinctly. Yeah, it's what you don't want, but for a moderate amount of fields this might be ok.

  3. Use a macro as others suggested. However I think it's best to avoid macros whenever possible. It isn't worth it.

It is not easy nor good to hack something like this together in C (there is no duck typing). This is a typical example where a dynamic language would be more comfortable (maybe you could call python from your code? :) )

这篇关于需要改变不同结构中常见字段的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆