通过结构指针访问结构变量分配错误的数据

时间:2012-12-05 17:10:45

标签: c++

/* c++ file */
extern "C"
{
#include "Param.h"
#include "manager.h"      
}
void Manager::Init(){

    struct config pParam;   
    memset(&pParam, 0, sizeof(pParam));

    pParam.pulse = 123;
    pParam.rotation = 567;

    Parameters(&pParam);
}

/* c file */
int max_pulse = 0, rotation = 0;
void Parameters(const struct config *p_param)
{
    max_pulse   = p_param->pulse; // assign the wrong data here
    rotation    = p_param->rotation; // assign the wrong data here
}

这是一个非常奇怪的问题。

config在Param.h中定义,管理器类在manager.h文件中定义。

运行代码后,我得到max-pulse = 567和rotation = 0。 我不知道为什么这个代码会发生这种情况。我正在使用visual studio 2008 express。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您收到错误可能是因为您正在使用Parameters()参数调用pParam函数,而您正在初始化p_param成员。

相关问题