[英]Pointer type of smaller size than variable
Working with embedded systems, in order to have more resolution in a incremental sequence, I have two variables, one always following the other. 为了在增量系统中具有更高的分辨率,在嵌入式系统中工作,我有两个变量,一个总是跟随另一个。
Specifically, I set a goal value using a 8 bits variable, but to go from one point (current value) to another I do it using 32 bits steps. 具体来说,我使用8位变量设置了目标值,但要从一个点(当前值)转到另一个点,则使用32位步长进行设置。
For example (that is a stupid example, but it just to show how I want to use it, in my code there are some temporizations which require the 32 bits varaibles to allow a slow change): 例如(这是一个愚蠢的示例,但是它只是为了展示我要如何使用它,在我的代码中有一些临时方法需要32位变量来允许缓慢的更改):
/* The variables */
char goal8bits; // 8 bits
long int current32bits; // 32 bits
char current8bits; // 8 bits
long int step32bits; // 32 bits
/* The main function (in the real code that is done periodically with a specific period) */
current32bits = CONVERT_8BITS_TO_32BITS(current8bits); // E.g: 0xAB -> 0xABABABAB
if (goal8bits < current8bits) {
current32bits += step32bits;
}
current8bits = CONVERT_32BITS_TO_8BITS(current32bits); // E.g: 0x01234567 -> 0x01
/* Other parts of the code */
I use current8bits to know the current value in the middle of a transition.
My question is if I can use a char pointer and make it point to the 32 bits variable one, so I do not need to update it each time I change it. 我的问题是,是否可以使用char指针并将其指向32位变量1,所以我不需要在每次更改它时都进行更新。 The previous example will look like this:
前面的示例将如下所示:
/* The variables */
char goal8bits; // 8 bits
long int current32bits; // 32 bits
char *current8bits = (char *)¤t32bits; // Pointer to 8 bits
long int step32bits; // 32 bits
/* The main function (in the real code that is done periodically with a specific period) */
if (goal8bits < *current8bits) {
current32bits += step32bits;
}
/* Other parts of the code */
I will use *current8bits to know the current value in the middle of a transition.
Do you see any problem in doing that? 这样做有什么问题吗? Can it lead to a problem wih endianism?
它会导致字节序问题吗?
Thank you! 谢谢!
Yes, it is endian dependent code, to make it portable you can use a mask and the left shift operator: 是的,它是与字节序相关的代码,可以使用掩码和左移运算符来使其可移植:
uint8_t goal8bits = 0x01; // 8 bits
uint32_t current32bits = 0x01234567; // 32 bits
uint32_t step32bits = 1; // 32 bits
if (goal8bits < ((current32bits & 0xFF000000) >> 24)) {
current32bits += step32bits;
}
If you know the endianless of your system, and it is static you have to select from 如果您知道系统的字节序,并且它是静态的,则必须从
char *current8bits = (char *)¤t32bits;
or 要么
char *current8bits = (((char *)¤t32bits)+3);
If you have to test it, and your system cannot give you such of info you can derive it at application startup 如果您必须对其进行测试,并且系统无法提供此类信息,则可以在应用程序启动时派生它
uint32_t temp = 0x01020304;
uint8_t *temp2 = (uint8_t *)(&temp);
if (*temp2 == 0x01)
{
char *current8bits = (char *)¤t32bits;
}
else
{
char *current8bits = (((char *)¤t32bits)+3);
}
Another good solution is the top-voted and checked-as-answered answer HERE . 另一个很好的解决方案是HERE投票最多并已回答的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.