-2

I have string of strings that I want send to a function and change the strings of the string.

How do I send the string to the function ? With 2 * or with 3 *? For example:

I want change and add the str[0] and str[1]. How do I send it to the function? Also, how do I send it to freeStr? Is it the the same as sending it to the change function?

int main(void)
{
    char **str = (char **)malloc(sizeof(char*) * 3);
    str[0] = (char *)malloc(sizeof(char) * 10);
    str[1] = (char *)malloc(sizeof(char) * 10);

    changeStr(str);
    freeStr(str);
}

void changeStr(/* ... */)
{
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • The computer will not blow up if you try different alternatives. And if you compile with `-Wall -Wextra` the compiler will most likely warn you if you do anything wrong. – klutt Jul 22 '20 at 08:17
  • 3
    `free` what you `malloc` (or `calloc` or even maybe `realloc`) .... you have `str[0] = (char*)malloc(sizeof(char)*10);` so you need `free(str[0])` ... you have `char** str=(char**)malloc(sizeof(char*)*3);` so you need `free(str);` – pmg Jul 22 '20 at 08:18
  • 1
    The cast to the return value of `malloc()` is, at best, redundant, and it may hide an error the compiler would have caught otherwise. – pmg Jul 22 '20 at 08:24
  • Related: https://stackoverflow.com/questions/1733881/c-correctly-freeing-memory-of-a-multi-dimensional-array – Hulk Jul 22 '20 at 08:25
  • 2
    Unrelated: prefer `int main(void) { ... }` for your main function – pmg Jul 22 '20 at 08:25
  • Related: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Hulk Jul 22 '20 at 08:30
  • Also, make sure to also pass the length (in both dimensions) of the array to your function, otherwise you will have no way to safely access it. – Hulk Jul 22 '20 at 08:34

1 Answers1

-1

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.