简体   繁体   English

没有获得正确的结构价值

[英]Not getting correct value in structure

might be it's silly question but not getting what is wrong in my code. 可能这是一个愚蠢的问题,但没有弄清楚我的代码有什么问题。

I am writing simple command parser in which it will initialize appropriate parameters and call appropriate functions related to command code. 我正在编写简单的命令解析器,它将在其中初始化适当的参数并调用与命令代码相关的适当函数。

Ex. 防爆。

char buffer[]="1123,13,46";

In above line 1123 is command code and rest of two tokens are parameters. 在上面的行1123是命令代码,其余两个令牌是参数。 In parser it will first find command code form command table and if command found then it will initialize structure of that command with above parameters. 在解析器中,它将首先从命令表中找到命令代码,如果找到了命令,则它将使用上述参数初始化该命令的结构。

Currently in my code i am successfully fetching correct command from command table and call that command's functions but failed to initialize the parameters with above values (13 and 46 here).Always getting 0 . 目前,在我的代码中,我成功地从命令表中获取了正确的命令并调用了该命令的函数,但未能使用上述值(此处为13和46)初始化参数。始终为0

As per below code same thing i want in my command parser but little modification 按照下面的代码,我想要在命令解析器中做同样的事情,但几乎没有修改

typedef struct
{
    void *fields[2];

}tmpStruct;

typedef struct
{

    int x;
    int y;

}myStruct;

tmpStruct tmp_struct;

myStruct *getParams(tmpStruct *t_struct)
{
    myStruct *genericStruct = malloc(sizeof(myStruct));

        //setup the order of the fields in the handler
    t_struct->fields[0]=(void*)&genericStruct->x;

    t_struct->fields[1]=(void*)&genericStruct->y;

    return genericStruct;
}

void *fillValue(tmpStruct *t_struct)
{
    void *genericStruct;

    genericStruct = getParams(t_struct);

    *((int*)t_struct->fields[0])=12;
    *((int*)t_struct->fields[1])=13;

    return genericStruct;
}

void pritValue(myStruct *my_struct)
{
    printf("%d  %d\n",my_struct->x,my_struct->y);
}

int main()
{
    void *genericStruct;

    genericStruct = fillValue(&tmp_struct);

    pritValue(genericStruct);

    return 0;
}

Please find my working source file here 在这里找到我的工作源文件

And header file here 和头文件在这里

The problem is that you are casting function pointers to types of functions that take different parameter types. 问题在于您正在将函数指针转换为采用不同参数类型的函数类型。 Trying to make calls to functions through pointers like that is undefined behavior. 试图通过类似这样的指针来调用函数是未定义的行为。 Although you could potentially get away with calls that are similar - say, return myStruct* in place of void* pointer (although it's still undefined behavior), there is no way you could cast a function that takes an int and a myStruct* to a function type that takes an int and a variable list of arguments. 尽管您可能会避免使用类似的调用-例如,返回myStruct*代替void*指针(尽管它仍是未定义的行为),但是您无法将带有intmyStruct*的函数myStruct*转换为接受一个int和一个变量列表的函数类型。

Changing your functions to the correct signatures to match cmdTableStruct table fixes this problem: 将函数更改为正确的签名以匹配cmdTableStruct表可解决此问题:

void *setParams(paramHandler_type *pHandler);
int printParams(int commandType,... );

static cmdTableStruct cmdTable[]=
{
 { 1123,setParams,printParams },
 //more command in same manner
};

Demo. 演示。

Of course now you need to handle parameters from the ... list individually using the functionality from the <stdarg.h> header. 当然,现在您需要使用<stdarg.h>标头中的功能分别处理...列表中的参数。

Also, the way you dereference param handler when assigning values is incorrect - the index should be applied to paramValue , not to pHandler : 另外,分配值时取消引用参数处理程序的方式也不正确-索引应应用于paramValue ,而不是pHandler

*((int*)pHandler->paramValue[j]) = (int)atoi(token);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM