2016-04-23 14 views
0

私は4つの文字列を持っています。私はこのフォーマットで文字列を印刷したいと思います:文字列の配列を出力し、NULLを無視します

     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. 

はどのようにして印刷するときにNULLを無視して行くのでしょうか?私はこれを行う方法がわかりません。

+0

'strings_quadruples1_action [0] == NULL? 'strings_quadruples1_action [0]:" "' – adatapost

答えて

0

あなたは三項演算子を使用したい場合があります

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

は、他の3つの文字列のために同じことを行います。

それとも初めにマクロを定義:

#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])); 
関連する問題