[英]Error in printing out the data obtained in pointer to character strings
I am trying to call an interface function and print out the resulting values. 我正在尝试调用接口函数并打印出结果值。 The function prototype contains pointers to character string as arguments.
函数原型包含指向字符串的指针作为参数。 When I print out the resulting values I receive some strange symbols.
当我打印出结果值时,会收到一些奇怪的符号。 Is my below approach correct?
我的以下方法正确吗?
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);
Your printf is not that safe. 您的printf不太安全。 It should be :
它应该是 :
printf("%s", b); //corrected - safe!
In C++, you can also use std::cout
as: 在C ++中,您还可以将
std::cout
用作:
std::cout << b; //its simpler - must include<iostream>
Make sure that b
is a null-terminated c-string, otherwise it wil print garbage and may crash your program. 确保
b
是一个以null终止的c字符串,否则它将打印垃圾,并可能使程序崩溃。 Null-terminated string means it must end with \\0
. 空终止的字符串意味着它必须以
\\0
结尾。 Something like this: 像这样:
b[slen] = '\0'; //you may have to do this explicitly!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.