C concat

User avatar
arunbm123
Posts: 62
Joined: Fri Feb 23, 2018 5:36 am

C concat

Postby arunbm123 » Fri Mar 01, 2019 3:04 pm

hello ESP Geeks,

I am modifing a http_client example

const char *post_data = "field1=value1&field2=value2";
esp_http_client_set_url(client, "http://httpbin.org/post");

I want to concat Integer value to field1 like "field1=656565"
I am not able to do facing some problem due to my weakness with pointers.

Please Help..

ESP_igrr
Posts: 1415
Joined: Tue Dec 01, 2015 8:37 am

Re: C concat

Postby ESP_igrr » Fri Mar 01, 2019 3:31 pm

Something like

const char *post_data;

asprintf(&post_data, "field1=%d&field2=%d", value1, value2);

....

free(post_data);

(where value1 value2 are ints)

User avatar
arunbm123
Posts: 62
Joined: Fri Feb 23, 2018 5:36 am

Re: C concat

Postby arunbm123 » Sat Mar 02, 2019 3:42 am

hi

Thanks ! it works...

peterglen
Posts: 16
Joined: Thu Mar 22, 2018 5:55 pm

Re: C concat

Postby peterglen » Sat Mar 02, 2019 7:32 pm

If you want to use code that does not call malloc:

char tmp[32];

snprintf(tmp, sizeof(tmp), "%d %d", val2, val2); // val1 and val2 are your integers

User avatar
arunbm123
Posts: 62
Joined: Fri Feb 23, 2018 5:36 am

Re: C concat

Postby arunbm123 » Sun Mar 17, 2019 10:05 am

hello

asprintf(&post_data, "field1=%d&field2=%d", value1, value2);
....
free(post_data);

This is eating away Heap memory
Why free is not working ?

Return to “General Discussion”

Who is online

Users browsing this forum: nicola, TuxuT_ESP32 and 31 guests