Home > Back-end >  Sprintf splicing char *, null pointer is identified as "(null)"
Sprintf splicing char *, null pointer is identified as "(null)"

Time:09-29

RT...
For example:
Char a, [10]={0}.
Sprintf (a, "111% s222", b);
Which is char * type b, if b=NULL, vs2010 would result in 111 (NULL) "222", I expected result is "111222", could you tell me how to solve each master... The genuflect is begged! Thank you very much!

CodePudding user response:

Finally come to a string replacement, replace is empty (null)

CodePudding user response:


 
Sprintf (a, "111% s222", b? B: "");

CodePudding user response:

reference 1st floor ksrsoft response:
finally come a string substitution, empty (null) replaced by


Can't oh, if the string inside itself contains "(null)", will be replaced

CodePudding user response:

refer to the second floor 9527 response:
 
Sprintf (a, "111% s222", b? B: "");


Equivalent to made a judgment, be afraid influence the efficiency of...
Is there any other method? Still say sprintf is conducted to determine?

CodePudding user response:

reference 4 floor eagle229036533 response:
Quote: refer to the second floor 9527 response:


 
Sprintf (a, "111% s222", b? B: "");


Equivalent to made a judgment, be afraid influence the efficiency of...
Is there any other method? Still say sprintf is conducted to determine?

C a lot of use of pointer passed as a parameter, a lot of moment are not allowed to empty, so you should ensure that the value of b, not by the caller to judge:
1) in b sources to ensure that no b is empty, if subsequent calls for empty don't
2) if the guarantee is not, you can only call in judgment

CodePudding user response:

There is a format can be solved in the c + + builder
  • Related