Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable for #define

Tags:

c++

c

libcurl

Libcurl uses the following to define the email recipient:

#define RECIPIENT "<[email protected]>"

But what if I don't want to hard code the recipient? I want a user to be able to supply his/her own email address, so I need to find a way to do this:

std::string emailreceiver = "[email protected]";
#define RECIPIENT = emailreceiver

The recipient is used in this line:

rcpt_list = curl_slist_append(rcpt_list, RECIPIENT);

I'm assuming I can't simply change this to

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver);

Anyone have any suggestions?

like image 836
natli Avatar asked Dec 07 '22 18:12

natli


2 Answers

Curl expects a C string (const char *), not a C++ string (std::string). So try:

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver.c_str());

There's no need to use a #define at all, that was just an example.

like image 167
Greg Hewgill Avatar answered Dec 24 '22 18:12

Greg Hewgill


Your last snippet is probably pretty close. From the looks of things, curl is expecting a C-style string though, so you may have to change it to:

std::string emailreceiver = "[email protected]";
rcpt_list = curl_slist_append(rcpt_list, emailreceiver.c_str());
like image 28
Jerry Coffin Avatar answered Dec 24 '22 19:12

Jerry Coffin