Hi!
I will be grateful for your help on how a pointer to a pointer works as a parameter to a function. This pointer to a pointer topic gives me a truly hard time.
For example, there are such lines in the main function:
Code:
char* some_buffer;
int buffer_length;
if (some_function(&some_buffer, &buffer_length))
Then within some_function:
Code:
bool some_function (char** buffer_from_main, int* buffer_length)
{
if((*buffer_from_main = malloc(*buffer_length + 1)) != NULL)
do something and return true; else return false;
}
My questions:
If I understand it correctly, buffer_from_main is a pointer to a pointer; so buffer_from_main points to the address of the pointer some_buffer (which is in the main function), and some_buffer, in its turn, points to some address, where something should be stored, or is stored (I will refer to this something as "stored_value").
Thus if I pass char** buffer_from_main as a parameter of the function, do I pass the address of "stored_value"?
If yes, then
Code:
*buffer_from_main = malloc(*buffer_length + 1)
allocates memory not for the "stored_value" but for the pointer
char* some_buffer? And to allocate a memory for "stored_value", I have to write
Code:
if((**buffer_from_main = malloc(*buffer_length + 1)) != NULL)
correct?
Thanks a lot for your help!