最近同学叫我帮忙看个问题,为啥这个循环没有退出,

代码如下,原本是想拿到最后的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))

最新文章

  1. [LeetCode] Sentence Screen Fitting 调整屏幕上的句子
  2. npm isArray源码
  3. 自话自说——POI使用需要注意一个地方
  4. dbVisualizer连接mysql
  5. 简单几何(凸包) POJ 1113 Wall
  6. PHP实例开发(2)PHP通过mail()或Socket发邮件
  7. Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
  8. Journal.Today 1.0.0
  9. 删除Windows右键不用的选项
  10. java命名规则
  11. 使用了hibernate时候乱码问题
  12. web字体格式转换
  13. js正则表达式中的问号使用技巧总结
  14. javascript call()函数
  15. Fluent Validation with Web Api 2
  16. 浅探element-ui2组件源码之upload
  17. Java的字符串分割的不同实现
  18. 爬虫之scrapy-splash
  19. Ajax技术使用补充
  20. Fluent动网格【12】:扩散光顺

热门文章

  1. 一文彻底搞懂Hive的数据存储与压缩
  2. 剑指offer计划20( 搜索与回溯算法中等)---java
  3. 【PHP数据结构】链表的相关逻辑操作
  4. php 解决返回数据 数字 变成科学计数法后转换问题
  5. curl 理解
  6. 【译】.NET Core 3.0 发布小尺寸 self-contained 单体可执行程序
  7. P7515-[省选联考 2021A卷]矩阵游戏【差分约束】
  8. P7726-天体探测仪(Astral Detector)【构造】
  9. hdu5909-Tree Cutting【FWT】
  10. Hutool-Convert类型转换常见使用