简体   繁体   English

C中的结构作为函数返回值

[英]Structures in C as function return value

i would like to use strucs in plain C to manage Data on a microcontroller. 我想在普通C语言中使用strucs在微控制器上管理数据。 I read a lot of online tutorials, but could not find the solution to my problem. 我阅读了很多在线教程,但是找不到解决我问题的方法。 The µC always runs into HardFault . µC总是会遇到HardFault

This is the struct definition in the header file: 这是头文件中的结构定义:

typedef struct{
  uint8_t laenge;
  uint8_t rfu;
  uint16_t nsi;
  uint8_t epc_data[24];
  uint16_t crc16;
}epc_memory;

this is the function prototype : 这是函数原型:

epc_memory decodeACK(uint32_t rxBuffer[]);

The function : 功能 :

epc_memory decodeACK(uint32_t rxBuffer[])
{
counter=0;
epc_memory mem;
//Anfang finden
while(rxBuffer[counter] > ERROR_) counter++;
makeString(rxBuffer,counter);
startEnding();
counter = decoder();
if(counter > 100){

        for (counter=0; counter<4; counter++)
        {
            mem.laenge = (mem.laenge << 1) | decoded[counter];
        }

        for (counter=4;counter<6; counter++)
        {
            mem.rfu = (mem.rfu << 1) | decoded[counter];
        }

        for (counter=6;counter<15; counter++)
        {
            mem.nsi = (mem.nsi << 1) | decoded[counter];
        }

        for(counter=15;counter<(15+mem.laenge);counter++)
        {
            mem.epc_data[(counter-15)/4] = (mem.epc_data[(counter-15/4)] << 1) | decoded[counter];
        }

        for(counter=(15+mem.laenge);counter<(31+mem.laenge);counter++)
        {
            mem.crc16 = (mem.crc16 << 1) | decoded[counter];
        }


    }

return mem;

 }

And this is how it is called in the main : 这就是它在主体中的调用方式:

epc_memory tmp = decodeACK(&rxBuffer);

Can you spot any errors ? 您能发现任何错误吗?

Thanks 谢谢

edit: just for reference... THIS function works just fine : 编辑:仅供参考...此功能工作正常:

uint16_t decodeRN16(uint32_t rxBuffer[])
{
counter=0;
uint16_t rn16 = 0;
//Anfang finden
while(rxBuffer[counter] > ERROR_) counter++;
makeString(rxBuffer,counter);
startEnding();
counter = decoder();
if(counter > 16){

    for (counter=0; counter<16; counter++)
    {
        rn16 = (rn16 << 1) | decoded[counter];
    }

}

return rn16;

}

The code as shown misses to initialise mem before reading it's members' values. 所示代码在读取成员值之前未初始化mem This might very well cause undefined behaviour. 这很可能会导致不确定的行为。 One outcome then could be a "hard fault". 那么一个结果可能是“硬故障”。

To fix this replace 要修复此替换

epc_memory mem;

by 通过

epc_memory mem = {0};

This latter statement intialises all mem 's members to 0 . 后一个语句将所有mem的成员初始化为0


As Jean-François Fabre deleted his answer I want additionally point out that 让·弗朗索瓦·法布尔删除他的答案时,我还想指出

epc_memory decodeACK(uint32_t rxBuffer[])

if called 如果叫

decodeACK(&rxBuffer);

is called wrongly. 被错误地称为。

Assuming the array's defintion to be 假设数组的定义是

  uint32_t rxBuffer[1024];

call the function 调用函数

  decodeACK(rxBuffer);

Background 背景

 epc_memory decodeACK(uint32_t rxBuffer[])

is the same as 是相同的

 epc_memory decodeACK(uint32_t * rxBuffer)

and passing an array to a function makes it decay to the address of its 1st element. 并将数组传递给函数会使它衰减到其第一个元素的地址。 Here this will be uint32_t* . 这里是uint32_t*

Passing &rxBuffer would evaluate to pass a pointer to an array of 1024 uint32_t , which would be a uint32_t(3)[1024] , which clearly is not what decodeACK() expects. 传递&rxBuffer会评估为将指针传递给1024 uint32_t数组,该数组将是uint32_t(3)[1024] ,这显然不是decodeACK()期望的。

The reason it still might seem to work sometimes, is that the address of an array in fact is the same address as the address of it's 1st element. 它有时似乎仍然起作用的原因是,实际上数组的地址与其第一个元素的地址相同。

Still passing the array's address here is passing the wrong type, is invoking the infamous Undefined Behaviour, is unreliable code, is wrong code, which the compiler clearly should have pointed you to by issuing a related warning. 仍然在此处传递数组的地址正在传递错误的类型,正在调用臭名昭著的Undefined Behaviour,是不可靠的代码,是错误的代码,编译器显然应该通过发出相关警告来向您指出。

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

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