我在 C 代码中经常使用字符串文字表。这些表或多或少都像这样:
static const char* const stateNames[STATE_AMOUNT] =
{
"Init state",
"Run state",
"Pause state",
"Error state",
};
Run Code Online (Sandbox Code Playgroud)
上面代码的问题是,如果表变长了,在开发过程中修改了,我会时不时忘记逗号。代码编译没有问题,缺少逗号,但我的程序最终崩溃,因为最后一个字符串设置为NULL
. 我使用了 MinGW 和 Keil 编译器来验证。
如果缺少逗号,有没有办法为我的初始化生成编译器警告?
我正在尝试打开二进制文件进行写入而不删除内容.但我不想写给eof.我想写一个文件中的特定位置.
这是一个小例子:
ofstream out("test.txt", ios::binary | ios::app);
for(int i = 0; i < 100; i++)
out.put('_');
out.write("Hallo", 5);
out.close();
ofstream out2("test.txt", ios::binary | ios::app);
out2.seekp(10);
out2.write("Welt", 4);
out2.close();
Run Code Online (Sandbox Code Playgroud)
如果使用app,搜索不起作用.如果不使用app打开文件擦除数据.有人知道答案吗?
我尝试在 stm32f4 上实现 i2c 从接收器中断服务例程。这是我的智能代码。
void I2C2_EV_IRQHandler()
{
switch (I2C_GetLastEvent(I2C2))
{
//The address sent by the master matches the own address of the peripheral
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
//The slave stretches SCL low until ADDR is
//cleared and DR filled with the data to be sent
I2C_ClearFlag(I2C2,I2C_FLAG_ADDR);
break;
//The application is expecting a data byte to be received
case I2C_EVENT_SLAVE_BYTE_RECEIVED:
I2C_ReceiveData(I2C2);
break;
//The application is expecting the end of the communication
//Make sure that both ADDR and STOPF flags are cleared
//if …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 set /p 命令在循环中调整 Windows 批处理变量。键盘输入后,变量仍包含旧值。我读过通过 set /p 设置的变量只有局部范围。但我不明白“本地”在这里的真正含义。
@echo off
setlocal EnableDelayedExpansion
set a=4
echo Inital A: %a%
:LoopLabel
MODE | find %a% >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
set /p "a=enter new a: "
echo a=%a%
goto LoopLabel
)
Run Code Online (Sandbox Code Playgroud)
输出是:
Inital A: 4
enter new a: 5
a=4
enter new a: 6
a=5
enter new a: 7
a=6
Run Code Online (Sandbox Code Playgroud)
有没有人有想法,我能解释一下为什么会这样吗?
非常感谢,强尼