结束的NULL
2024-08-31 02:08:58
最近同学叫我帮忙看个问题,为啥这个循环没有退出,
代码如下,原本是想拿到最后的NULL指针就可以结束循环
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h> typedef void (*Callback)(int);
void call_pin(int input);
void call_frequency(int input); struct CMD {
char* s; // short name
char* l; // long name
char c; // long name length(byte)
Callback call; // callback function
};
struct CMD *cmd = NULL;
struct CMD cmds[] = {
{"p", "pin", sizeof("pin"), call_pin},
{"f", "frequency", sizeof("frequency"), call_frequency},
NULL
}; void call_pin(int input)
{
printf("%s", __func__);
} void call_frequency(int input)
{
printf("%s", __func__);
} int main(int argc, char *argv[])
{
cmd = cmds;
do {
printf("%x\r\n", cmd);
printf("%x\r\n", *cmd);
} while (++cmd); return 0;
}
程序跑起来后while循环没有按照设想的推出,而是一直循环到段错误停止
++cmd在第三次并没有拿到NULL(就是0嘛) 拿到的应该是cmds第三个元素的地址,这个结构体元素的第一个数据是NULL。
后来想了一下如果循环改成
while(*(++cmd))
不久拿到了最后一个NULL了吗,但是代码在编译的时候就提示错误了。
RPiPWMH.c:40:14: error: used struct type value where scalar is required
} while (*(++cmd));
while循环判断条件需要的是一个纯标量,但是我们好像返回来的是个结构体
于是把这个转换char指针拿到第一个char的数据也就是NULL
while(*(char *)(++cmd))
最新文章
- [LeetCode] Sentence Screen Fitting 调整屏幕上的句子
- npm isArray源码
- 自话自说——POI使用需要注意一个地方
- dbVisualizer连接mysql
- 简单几何(凸包) POJ 1113 Wall
- PHP实例开发(2)PHP通过mail()或Socket发邮件
- Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
- Journal.Today 1.0.0
- 删除Windows右键不用的选项
- java命名规则
- 使用了hibernate时候乱码问题
- web字体格式转换
- js正则表达式中的问号使用技巧总结
- javascript call()函数
- Fluent Validation with Web Api 2
- 浅探element-ui2组件源码之upload
- Java的字符串分割的不同实现
- 爬虫之scrapy-splash
- Ajax技术使用补充
- Fluent动网格【12】:扩散光顺