Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

joining 2 const char*s c++

How can I combine two const char*s into a third?

i'm trying to do this with this code:

const char* pName = "Foo"
printf("\nMy name is %s.\n\n\n",pName);
const char* nName;
int num_chars = asprintf(&nName, "%s%s", "Somebody known as ", pName);

But I get this error:

'asprintf': identifier not found

I include stdio.h via this code:

#include <stdio.h>
like image 201
Luke Avatar asked Nov 29 '25 06:11

Luke


1 Answers

Simple, just use C++:

const char* pName = "Foo"
std::string name("Somebody known as ");
name += pName;

const char* nName = name.c_str();
like image 195
Luchian Grigore Avatar answered Nov 30 '25 20:11

Luchian Grigore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!