我想测试myCurrentPtr
是否指向数组a
内。
_B
表示a
中的值数。因此,a + _B
应指向数组的最新值。
#define _B ((uint8_t)5)
volatile uint8_t a[5] = {0, 1, 2, 3, 4}; //`a` is a pointer to the first array element
if (myCurrentPtr > (a + _B)) {
printf("Out of bounds!");
}
不编译。你有什么主意吗?
Whereas,
...
if (myCurrentPtr > (a + 5)) {
printf("Out of bounds!");
}
编译就可以了。
预处理后两个不是完全相同吗?
这在我的系统上有效(使用gcc和clang编译):
#include <stdio.h>
#include <stdint.h>
#define _B ((uint8_t)5)
int main(){
volatile uint8_t a[5] = {0, 1, 2, 3, 4}; //`a` is a pointer to the first array element
volatile uint8_t *myCurrentPtr = &a[2];
if (myCurrentPtr > (a + _B)) {
printf("Out of bounds!");
}
else
{
printf("In array!");
}
}
输出:
In array!