1

I would like to allocate memory in a function and then use that space in the main().
All is fine in the function, but I never can access the data in the main().
There are two memory allocation instructions for simulating a two dimensionnal array of chars

Here's my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int getNetworks(char **networks) {

  networks = malloc(5 * sizeof(char*));
  printf("networks in function:%p\n", networks);

  networks[0] = (char*) malloc(4);
  strcpy(networks[0], "aaa");
  networks[1] = (char*) malloc(3);
  strcpy(networks[1], "bb");
  networks[2] = (char*) malloc(5);
  strcpy(networks[2], "cccc");
  networks[3] = (char*) malloc(6);
  strcpy(networks[3], "ddddd");
  networks[4] = (char*) malloc(2);
  strcpy(networks[4], "e");
  return 5;
}

int main ()
{
  char **networks = NULL;
  int nbNetworks;

  printf("networks before call:%p\n", networks);
  nbNetworks = getNetworks(&*networks);
  printf("networks after call:%p\n", networks);
  for (int i=0; i<=nbNetworks-1; i++) {
    printf ("%s\n", networks[i]);
  }
  return 0;
}

Output is

networks before call:0x0
networks in function:0x7feb65c02af0
networks after call:0x0
Segmentation fault: 11
3
  • 1
    That should be *networks = malloc(5 * sizeof(char*)); because you want to modify the parent's variable, not the value parameter on the stack. Commented Mar 16, 2019 at 11:36
  • 1
    getNetworks(&*networks); smells bad. Commented Mar 16, 2019 at 11:37
  • In the same way it should be (*networks)[0] = (char*) malloc(4); as you are working with the parent's array. Commented Mar 16, 2019 at 11:38

2 Answers 2

4

The following needs to be fixed:

int getNetworks(char ***networks)

as it is a pointer to an array of pointers to chars. (You can also write this as
char **networks[]).

In the function you must first dereference the pointer to work on the parent's variable:

    *networks = malloc(5 * sizeof(char*));
    (*networks)[0] = (char*) malloc(4);

In main, your declaration is correct, however, you must call as:

    getNetworks(&networks);

so the function can work on the parent's variable.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked right away thanks!
1

There are many problems in your code, I will not comment them, but propose you this solution:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char** getNetworks(int *n)
{
  int count = 5;
  char ** networks = malloc(count * sizeof(char*));
  printf("networks in function:%p\n", networks);

  networks[0] = (char*) malloc(4);
  strcpy(networks[0], "aaa");
  networks[1] = (char*) malloc(3);
  strcpy(networks[1], "bb");
  networks[2] = (char*) malloc(5);
  strcpy(networks[2], "cccc");
  networks[3] = (char*) malloc(6);
  strcpy(networks[3], "ddddd");
  networks[4] = (char*) malloc(2);
  strcpy(networks[4], "e");
  *n = count;
  return networks;
}

int main ()
{
  char **networks = NULL;
  int nbNetworks;

  printf("networks before call:%p\n", networks);
  networks = getNetworks(&nbNetworks);
  printf("networks after call:%p\n", networks);
  for (int i=0; i < nbNetworks; i++) {
    printf ("%s\n", networks[i]);
  }
  return 0;
}

4 Comments

warning : malloc(*n * sizeof(char*)) while *n (in fact nbNetworks) is non initialized, move *n = 5; before (I am not the DV ;-) )
@bruno Thanks, corrected. I did not execute it, it was a mistake.
it works. Just a semicolon missing after "int count = 5". Nice solution.
@Noury ok, happy. I wrote the code directly in editor, so some bugs were present.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.