Page 1 of 1

C concat

Posted: Fri Mar 01, 2019 3:04 pm
by arunbm123
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..

Re: C concat

Posted: Fri Mar 01, 2019 3:31 pm
by ESP_igrr
Something like

const char *post_data;

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

....

free(post_data);

(where value1 value2 are ints)

Re: C concat

Posted: Sat Mar 02, 2019 3:42 am
by arunbm123
hi

Thanks ! it works...

Re: C concat

Posted: Sat Mar 02, 2019 7:32 pm
by peterglen
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

Re: C concat

Posted: Sun Mar 17, 2019 10:05 am
by arunbm123
hello

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

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