小编Wil*_*ult的帖子

是否可以“关闭”或“打开” C变量的波动性?

我在C中有两个静态的volatile变量,我想在一个逻辑语句中检查它们两个。但是,当我收到警告时,“未定义行为:此语句1037中未定义易失性访问的顺序”是否可以在很短的时间内暂停C变量的波动性以确保获得良好的数据?

这是代码:

static volatile unsigned char b;
static volatile unsigned char a;

//update the states of the two volatile variables 
update_vars( &a);
update_vars( &b);

// check them in a logical statement
// Can I suspend the volatile lable??  
if((addr_bit & (a | b)) == 0){
// update another variables
}
else{
// another action
}
Run Code Online (Sandbox Code Playgroud)

我在相同的中断环境中考虑此问题,但是如果希望在准确的时刻对数据进行稳定的评估,则可以暂时将其挂起。谢谢!

c warnings volatile

2
推荐指数
1
解决办法
70
查看次数

在C++中使用ofstream将32位二进制数据写入文件

我试图只写uint32_t数据到.bin文件.我首先打开一个文件流,我声明它是一个二进制文件流.

  `hartInStream.open (hartFileToWrite.c_str(), std::ifstream::binary);
   if(!hartOutStream.is_open())
        {
        printf("Something in outstream went wrong.\n");  
        }
   else 
        {
        printf("Yippy! it outstream worked!\n");  
        printf("---------Starting to read-------------\n");}`
Run Code Online (Sandbox Code Playgroud)

接下来,我将我尝试编写的数据(在本例中为uint32_t"data")打印出来,然后将其写入文件流的末尾.

printf(" Reading %lu from HART at %d-%d-%d\n", data, (int)addr1, (int)addr2, (int)addr3 );
Run Code Online (Sandbox Code Playgroud)

最后,我追加数据

hartOutStream<<data;
Run Code Online (Sandbox Code Playgroud)

我知道我应该写0x00 0x00 0x01 0x00(256)作为前4个字节,但我写的是32 35 36,这是ascii章程的十六进制等效为256.(使用HxD文件查看器)

我怀疑问题出现在<<运算符中,它将其强制转换为ascii.我尝试过类型转换,atoi()函数但似乎没什么用.有谁知道这有什么问题吗?

谢谢

c++ rhapsody

1
推荐指数
1
解决办法
1917
查看次数

是否可以使用C中的指针数学访问结构中的数据?

是否可以使用指针数学来移动结构并将数据分配给其成员?我有一个只有uint8_t数据的大型结构,并且编写结构的所有这些成员的名称是很多工作.我希望我可以只指定一个指向第一个数据的指针,然后将8添加到该地址.这可能吗?

/* All pieces of data are the same size */
typdef struct 
{
    uint8_t a;
    uint8_t b;
    uint8_t c:
}Mydata;

int main(void){

    /* New struct created */
    Mydata NewData;
    /* Pointer P points to the address of "NewData"*/
    uin8_t *p = &NewData;

    *p = 5;
    p = p + 1; // increment 1 byte to next address where data is stored
    *p = 7;
    p = p + 1;
    *p = 10;

return 0;
}
Run Code Online (Sandbox Code Playgroud)

c struct pointers

0
推荐指数
1
解决办法
53
查看次数

标签 统计

c ×2

c++ ×1

pointers ×1

rhapsody ×1

struct ×1

volatile ×1

warnings ×1