Code edit: https://gcc.godbolt.org/z/x3hnj46WY
Scenario -1 Trying to pass raw pointer by reference (or pointer) can't pass value of get() using & to setBuffer() // Compilation error : lvalue required as unary '&' operand
Scenario -2 Assign value return by get to a raw char pointer Pass char pointer to setBuffer() using &
NOTE: setBuffer() is present in a C library I am using this API https://www.libssh2.org/libssh2_session_last_error.html to get errmsg and instead of using a char array buffer I want to use smart pointer of char array.
#include <string.h>
#include <iostream>
#include <memory>
int setBuffer(char ** ptr) {
// ASSUMPTION: *ptr has sufficient memory to store copied data
strcpy(*ptr, "sample string");
return 0;
}
int main()
{
std::unique_ptr<char []> lup = std::make_unique<char []>(1024);
memset(lup.get(), 0 , 1024);
strcpy(lup.get(), "sample string - 1");
std::cout << lup << '\n'; // c++20
// SCENARIO - 1 | Compilation error
setBuffer(&lup.get()); // CE: lvalue required as unary '&' operand
// SCENARIO - 2 | Works fine
char * lp = lup.get();
setBuffer(&lp);
std::cout << lup << '\n'; // c++20
return 0;
}