-3

I have this struct

typedef struct{
 char **palavras;
}no;

and I want to allocate memory to this array of strings basically

and I can't do this, since it says it expects something before the '('

no *atual;

atual->(*palavras)=calloc(1,sizeof(char*));
João Gomes
  • 81
  • 10
  • Possible duplicate of [Allocate memory 2d array in function C](http://stackoverflow.com/questions/15062718/allocate-memory-2d-array-in-function-c) – Ken Y-N May 20 '16 at 01:07
  • @KenY-N: Althought the linked question uses the term "2D array", this is not a 2D array. As we don' know what OP intends, it is hasrd to say if he wants a 2D array or the pointer to pointer construct wich is **not** a 2D array, nor can it point to one. – too honest for this site May 20 '16 at 03:21

2 Answers2

2

You need to do it in several stages:

  • First, allocate memory to atual,
  • Next, allocate memory to palavras
  • Finally, allocate memory to elements of palavras

Assuming that you need to allocate 10 palavras, you can do it like this:

no *atual = malloc(sizeof(no));
atual->palavras = malloc(sizeof(char*)*10);
atual->palavras[0] = malloc(20);
...
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • oh thank you I get it, I was doing the oppossite, I tought you allocate memory for palavras with **, my bad yeah :/ – João Gomes May 20 '16 at 01:11
-1

You should access palavras by atual->palavras, e.g. atual->palavras = calloc(5, sizeof(char *)), and dereference the char ** by *atual->palavras. You may also allocate memory for the char * pointers by atual->palavras by, say, atual->palavras[0] = malloc(10 * sizeof(char)).

pjhades
  • 1,948
  • 2
  • 19
  • 34