[英]Error in printing out the data obtained in pointer to character strings
我正在嘗試調用接口函數並打印出結果值。 函數原型包含指向字符串的指針作為參數。 當我打印出結果值時,會收到一些奇怪的符號。 我的以下方法正確嗎?
int interfacecall(char *a, char *b ...);
char * a= "Testdata";
char b [4098];
result= interfacecall(a,b);
//different value is returned in b via function implementation in a third party dll.
printf(b);
您的printf不太安全。 它應該是 :
printf("%s", b); //corrected - safe!
在C ++中,您還可以將std::cout
用作:
std::cout << b; //its simpler - must include<iostream>
確保b
是一個以null終止的c字符串,否則它將打印垃圾,並可能使程序崩潰。 空終止的字符串意味着它必須以\\0
結尾。 像這樣:
b[slen] = '\0'; //you may have to do this explicitly!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.