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

accessing variable of structure through structure pointer assign wrong data

本文关键字:结构 分配 错误数据 变量 指针 访问      更新时间:2023-10-16
/* 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 文件中定义。

运行代码后,我得到最大脉冲 = 567 和旋转 = 0。我不知道为什么这段代码会发生这种情况。我正在使用Visual Studio 2008 express。

任何人都可以帮助我解决这个问题吗?

您收到错误可能是因为您在初始化p_param成员时Parameters()参数pParam函数。