问题描述
我在遇到一些麻烦一个指针声明,我的同事的人想的,因为MISRA C的要求使用。米斯拉(安全关键指标)不会让我们单纯的程序员使用指针,反而会让我们的字节数组操作。他打算指针procur以字节数组(所以我们没有在栈上传递的实际阵列。)
I'm having some trouble with a pointer declaration that one of my co-workers wants to use because of Misra C requirements. Misra (Safety Critical guideline) won't let us mere Programmers use pointers, but will let us operate on arrays bytes. He intends to procur a pointer to an array of bytes (so we don't pass the actual array on the stack.)
// This is how I would normally do it
//
void Foo(uint8_t* pu8Buffer, uint16_t u16Len)
{
}
// This is how he has done it
//
void Foo(uint8_t (*pu8Buffer)[], uint16_t u16Len)
{
}
调用函数看起来类似;
The calling function looks something like;
void Bar(void)
{
uint8_t u8Payload[1024]
uint16_t u16PayloadLen;
// ...some code to fill said array...
Foo(u8Payload, u16PayloadLen);
}
但是,当pu8Buffer在美孚(访问),该阵列是错误的。显然不是通过什么期待。该阵列是在调用函数正确的,但里面没有美孚()
But, when pu8Buffer is accessed in Foo(), the array is wrong. Obviously not passing what it is expecting. The array is correct in the calling function, but not inside Foo()
我觉得他创造了一个指针数组的字节,而不是一个指针字节数组。
I think he has created an array of pointers to bytes, not a pointer to an array of bytes.
有人在乎澄清?美孚(安培; u8Payload,u16PayloadLen);也不行。
Anyone care to clarify? Foo(&u8Payload, u16PayloadLen); doesn't work either.
推荐答案
在无效美孚(uint8_t有(* pu8Buffer)[],uint16_t u16Len)
, pu8Buffer
是一个指向 uint8_t有
的(不完全)阵列。 pu8Buffer
有一个不完整的类型;这是一个指向数组的大小是未知的。它可能不会在需要规模前pressions使用。(如指针运算; pu8Buffer + 1
是不允许的)
In void Foo(uint8_t (*pu8Buffer)[], uint16_t u16Len)
, pu8Buffer
is a pointer to an (incomplete) array of uint8_t
. pu8Buffer
has an incomplete type; it is a pointer to an array whose size is unknown. It may not be used in expressions where the size is required (such as pointer arithmetic; pu8Buffer+1
is not allowed).
然后 * pu8Buffer
是一个数组,其大小是未知的。因为它是一个数组,它会自动在大多数情况下转换为指针到它的第一个元素。因此, * pu8Buffer
成为一个指向第一个 uint8_t有
的数组。该类型的转换 * pu8Buffer
齐全;这是一个指向 uint8_t有
,因此它可能在地址运算中使用; *(* pu8Buffer + 1)
,(* pu8Buffer)[1]
和 1 [* pu8Buffer]
是对 uint8_t有
一举超越 * pu8Buffer $ C $的所有有效EX pressions C>。
Then *pu8Buffer
is an array whose size is unknown. Since it is an array, it is automatically converted in most situations to a pointer to its first element. Thus, *pu8Buffer
becomes a pointer to the first uint8_t
of the array. The type of the converted *pu8Buffer
is complete; it is a pointer to uint8_t
, so it may be used in address arithmetic; *(*pu8Buffer + 1)
, (*pu8Buffer)[1]
, and 1[*pu8Buffer]
are all valid expressions for the uint8_t
one beyond *pu8Buffer
.
这篇关于指针的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!