Archived Forum Post

Index of archived forum posts


Why does CkHttp_quickGetStr function returns a const char* ?

Oct 23 '15 at 11:49

Why does

const char *CkHttp_quickGetStr(HCkHttp cHandle, const char *url);

returns a const char and not a char ? And why can I still modify the return value if it's a const?

How does the CkHttp_quickGetStr handle internally the result from the HTTP request? Does it dynamically allocate memory for the response and it gets deallocated by calling CkHttp_Dispose(...)?

Accepted Answer

I assume this is for "C" programming?

Any method that returns a string has 2 forms. The lowercase form returns a "const char *" to internal memory. The uppercase form deposits the string in the CkString object that is passed in the last argument.

For example:

BOOL CkHttp_QuickGetStr(HCkHttp cHandle, const char *url, HCkString outStr);

const char CkHttp_quickGetStr(HCkHttp cHandle, const char url);

The uppercase form might be used like this:

HCkString strObj;
HCkHttp http;
BOOL success;
const char *html;

strObj = CkString_Create(); http = CkHttp_Create(); success = CkHttp_quickGetStr(http,"",strObj); html = CkString_getStringUtf8(strObj);


CkHttp_Dispose(http); CkString_Dispose(strObj);

If using the lowercase form, then be sure to use the string pointed to by the returned "const char *" before the CkHttp object is disposed, and before calling other methods. (Actually, you can call up to 10 methods before the least recent "const char *" is recycled.) If your app needs to keep a copy of the string, it should strcpy it to somewhere else. (or you can use the uppercase version of the method and just keep the strObj around..)