c - 打印字符串数组并忽略空值

标签 c

我有四个字符串数组。我想用这种格式打印出字符串:

                        printf(" %d \t %s \t %s \t %s \t %s.\n",
                        quadruples_line_counter,
                        strings_quadruples1_action[0],
                        strings_quadruples2_variable1[0],
                        strings_quadruples3_variable2[0],
                        strings_quadruples4_temp_variable[0]);

它给出了这个输出:

17      func    sub     int     3.
 17      param   (null)          (null)          (null).
 17      alloc           4       (null)          xa.
 17      alloc           4       (null)          y.
 17      alloc           4       (null)          z.
 17      multiply        55      y       t0.
 17      divide          t0      z       t1.
 17      plus            xa      t1      t2.
 17      plus            t2      x       t3.
 17      func    main    void    0.
 17      alloc           4       (null)          a.
 17      alloc           4       (null)          b.
 17      alloc           4       (null)          c.
 17      arg             (null)          (null)          x.
 17      arg             (null)          (null)          y.
 17      arg             (null)          (null)          z.
 17      call            sub     3       t5.
 17      assign          t5      (null)          y.

打印时如何忽略空值?我不知道该怎么做。

最佳答案

您可能希望使用三元运算符:

strings_quadruples1_action[0]!=NULL?strings_quadruples1_action[0]:""

对其他三个字符串也执行相同的操作。

或者在开头定义一个宏:

#define NO_NULL(X) X!=NULL?X:""

并执行 printf 操作:

printf(" %d \t %s \t %s \t %s \t %s.\n",
                        quadruples_line_counter,
                        NO_NULL(strings_quadruples1_action[0]),
                        NO_NULL(strings_quadruples2_variable1[0]),
                        NO_NULL(strings_quadruples3_variable2[0]),
                        NO_NULL(strings_quadruples4_temp_variable[0]));

关于c - 打印字符串数组并忽略空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806175/

相关文章:

在c中创建结构

c - 如何将参数从终端传递给函数

c - 如何在C语言中以指数形式(即6.6.7e-11)获得计算答案

c - 数组长度 (C)

c++ - 如何在 C++ (Linux) 中获得 CPU 时钟速度?

C 程序 - 值不添加到循环中的 vector

c - 使用for循环计算c中的字长时出现意外结果

c - fork 进程,信号量,为什么这个输出?

c++ - Z3:创建具有动态已知项数的枚举类型

c++ - CDT 定制构建