How do I send the string to the function? With 2 *
or with 3 *
?
First, The parameter at the definition/declaration of changePtr
has to be with two *
(char **
) or respectively with one *
and you address the amount of elements at an array declarator which gains also a pointer to pointer to char
(char *p[] == char **p
).
// Definition of changeStr.
void changeStr (char** p)
{
// some stuff going on here.
}
OR
// Definition of changeStr.
void changeStr (char* p[])
{
// some stuff going on here.
}
OR
// Definition of changeStr.
void changeStr (size_t elem, char* p[elem])
{
// some stuff going on here.
}
Triple pointers are considered as bad practice and one of them is not really needed here. It would make things even more complicated.
You call the function like this
changeStr(str);
for 1st and 2nd case, and
changeStr(sizeof(str) / sizeof(*str), str);
for the third case.
For the last case, The first argument evaluates the amount of elements, the second passes the value of the pointer to pointer to char
str
by value to changeStr
.
Also, how I send it to freeStr
? The same as I send to the change function?
Yes, but inside of the function you need to deallocate the allocated space for each separate string first. After that you can free()
the array of pointer to char
str
. If you free()
the array of pointer to char
first you ain't got the possibility to free the memory of the strings anymore and thus you have a memory leak.