gpt4 book ai didi

C:是否可以将指针关联到结构成员?

转载 作者:行者123 更新时间:2023-11-30 15:24:19 26 4
gpt4 key购买 nike

我想创建类似映射数组的东西,以便将来轻松开发某些模块。这个想法是让一个由外部函数执行的字符串,该函数总是返回一个字符串,该结果需要解析为正确的格式,然后将其存储在 Structure() 的特定成员中。使用特定的函数很容易做到这一点,但我想使用更通用的方法和函数,比如 atoi(或其重新创建的版本),如果可能的话,以便将来实现可移植性和快速实现。我创建了以下结构来保存此类映射数据:

typedef struct sMapping
{
const char *string;
int (* function)();
void *storage;
} tMapping;

然后创建一个这样的数组,将某些内容映射到前两个值:

tMapping mapTest[]={
{ .string = "name", .function = te_strcpy},
{ .string = "age", .function = te_atoi},
{ .string = "location", .function = te_parse},
{ .string = "height", .function = te_atoi}
};

我想要在代码中包含这样的内容:

char *outputstr = NULL;
for(i = 0; i<MAX_LEAFS; i++)
{
if(mapTest[i].string == NULL)
break;

outputstr = fct_lookup_option_string(mapTest[i].string);
mapTest[i].function(outputstr);
}

到目前为止,一切正常,一切都解析得很好。请注意,te_atoi 函数提取了 2 个变量。

现在如果我有另一个结构,例如

typedef struct sSave
{
char* name;
int age;
struct geo *location;
int height;
} tSave;

tSave person;

并且想要将某些内容映射到它

(CODE BELOW IS NOT CORRECT, I KNOW, JUST WANT TO PRESENT MY IDEA BETTER)
tMapping mapTest[]={
{ .string = "name", .function = te_strcpy, .storage= &(person.name)},
{ .string = "age", .function = te_atoi, .storage= &(person.age)},
{ .string = "location", .function = te_parse, .storage= &(person.location)},
{ .string = "heigth", .function = te_atoi, .storage= &(person.height)},
};

使用类似的东西

(CODE BELOW IS NOT CORRECT, I KNOW, JUST WANT TO PRESENT MY IDEA BETTER)
mapTest[i].storage = mapTest[i].function(outputstr);

这可能吗?有任何想法吗?双指针? C++?魔法?黑魔法?

谢谢。

请记住,我想将其用作多个模块(甚至可能是一些自动化)的通用模板,因此特定功能会对此进行更多限制。

最佳答案

非常有趣的项目。它一定会激励你去努力。

你的想法看起来不错,不需要 C++,但黑魔法可能会有所帮助:-)。

说实话:是的,可以将指针关联到结构成员。对于 char、short、int 和 long,我认为你这样做的方式是可以的。

对于字符串和结构,解析器必须分配内存。对于字符串,用 te_strdup 替换 te_strcpy 应该可以。

对于结构来说,情况有点复杂:您必须告诉解析器用于内存分配的结构的大小,并描述结构的内容才能正确填充它。为此,一种解决方案可能是在 tMapping 结构中添加这些信息:

typedef struct sMapping
{
const char *string;
int (*function)(char*, void*);
void *functionParam; // for a te_parse function this param could be a struct containing a tMapping array and a target struct to fill
void *storage;
} tMapping;

mapTest 数组变为:

tMapping mapTest[]={
{ .string = "name", .function = te_strdup, .storage= &(person.name)},
{ .string = "age", .function = te_atoi, .storage= &(person.age)},
{ .string = "location", .function = te_parse, .functionParam=sMapGeo, .storage= &(person.location)},
{ .string = "height", .function = te_atoi, .storage= &(person.height)},
};

要拥有通用的 te_parse 函数,sMapGeo 结构可以是:

(THIS CODE WON'T WORK AS IS, BUT IT EXPLAINS THE IDEA)
struct {
void *structToFill = &saveGeo;
int structSize = sizeof(saveGeo);
tMapping mapStruct[] =
{ .string="city", .function=te_strdup, .storage=&(saveGeo.city)}
etc.
} sMapGeo;

那么 te_parse 函数应该如下所示:

void *te_parse(char *input, tParseMap *sMap)
{
applyMapping(sMap->mapStruct); //apply the mapping like the main mapping
char *mem = malloc(sMap->structSize);
memcpy(mem, sMap->structToFill, sMap->structSize));
return mem;
}

最后一件小事。由于存储是一个指针,因此不要忘记 * 来为所指向的内容赋值:

*mapTest[i].storage = mapTest[i].function(outputstr, mapTest[i].functionParam);

毕竟,处理 float 、 double 、数组,甚至使代码与 32 和 64 位架构兼容都是另一回事了!

关于C:是否可以将指针关联到结构成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28477047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com