Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
combine c++ string with dynamically allocated c array of chars through overloaded add operator
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 [v] //clang 3.8.0 [v] //g++ 5.4.0 [v] //combine c++ string with dynamically allocated c array of chars through overloaded add operator // C++ headers #include <iostream> #include <string> // C headers #include <stdlib.h> #include <string.h> /** test function simulating data read * (fills the buffer "*pdata" with something * and the buffer "*dataSize" with its size */ void getData(char** pdata, int* dataSize); /** simply releases the data get with "getData()" */ void freeData(char** pdata); int main() { std::string s("my text is: "); // C++ std::string std::string t; // result char* data; // array of chars int siz; // the size of the array of chars // getData(&data, &siz); t = s + "\"" + data + "\""; std::cout<<t<<std::endl; // print result freeData(&data); // free resource(s) // return 0; } void getData(char** pdata, int* dataSize) { const char sz[] = "the quick brown fox"; *dataSize = strlen(sz); *pdata = (char*)malloc(*dataSize); memset(*pdata, 0, *dataSize); memcpy(*pdata, sz, *dataSize); } void freeData(char** pdata) { free(*pdata); }
run
|
edit
|
history
|
help
0
Variadic template example
Addition of two matrix **Part 2
1
Fractional Knapsack
LP
Sample Code from Scott Meyer's Blog
Matrix spiral print
palindrome
NWD
Preference List