简体   繁体   中英

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. It should be :

printf("%s", b);  //corrected - safe!

In C++, you can also use std::cout as:

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. Null-terminated string means it must end with \\0 . Something like this:

b[slen] = '\0'; //you may have to do this explicitly!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM