title: Realloc a structure in c
body: <p>So what I try to do is to dynamically allocate memory for a structure. The structure looks like this:</p>
  
  <pre><code>typedef struct{
     char name[N];
     int dimToys;
     TOY toys[N];
  }CITY;
  </code></pre>
  
  <p>I read some input from a .txt file and copy it in my structure.</p>
  
  <p>Then I have a function in which I would like to reallocate memory(and do some other irrelevant stuff) but my program works only when I use this function one single time. If I try to realloc the memory multiple times, then my program crashes.</p>
  
  <p>It looks something like this:</p>
  
  <pre><code>void next_city(CITY** cityList, *dimCities, otherstuff){
      (*dimCities)++;
      *cityList = realloc(*cityList, (*dimCities) * sizeof(CITY*));
      otherstuff...
  }
  </code></pre>
  
  <p>I tried running the debugger and it crashes at the line with realloc, when I call the function for the second time.
  The call of the function in the main function looks like this:</p>
  
  <pre><code> cityList = malloc(sizeof(CITY*));
   for(...){
       ...
       next_city(&amp;cityList, &amp;dimCities, otherstuff);
   }
  </code></pre>
  
  <p>I've already tried using a temporary variable for the reallocation and then copy it in my original <code>cityList</code>, but it doesn't work neither.</p>
  
  <p><strong>LATER EDIT:</strong></p>
  
  <p>Because a lot of people told me to update my question with some clarifications, I'll just show my code more clear, after I made some modifications from what you all told me.</p>
  
  <pre><code>void next_city(char line[], CITY **cityList, int *dimCities){
  (*dimCities)++;
  *cityList = realloc(*cityList, (*dimCities) * sizeof(CITY)); //UPDATE CITYLIST DIMENSION
  cityList[(*dimCities) - 1]-&gt;dimToys = 0;
  
  char *word = strtok(line, " ");
  strcpy((cityList[(*dimCities) - 1]-&gt;name), word); //INSERET NAME OF THE CITY
  word = strtok(NULL, " ");
  strcpy((cityList[(*dimCities) - 1]-&gt;toys[cityList[(*dimCities) - 1]-&gt;dimToys].toyType), word); //INSERT NAME OF THE TOY
  </code></pre>
  
  <p>}</p>
  
  <pre><code>int main(){
  int nrPasi;
  int dimCities = 0;
  scanf("%d", &amp;nrPasi);
  fgetc(stdin);
  int i;
  CITY *cityList;
  cityList = NULL;
  
  char line[100];
  for(i = 0;i &lt; nrPasi;i++){
      fgets(line, 100, stdin);
      next_city1(line, &amp;cityList, &amp;dimCities);
  }
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>So I basically read something like</p>
  
  <pre><code>3 
  Berlin car
  Berlin doll 
  Madrid jacket
  </code></pre>
  
  <p>And I just want to read it step by step.
  After I switched CITY* with CITY now realloc doesn't break my program, but it happens on the next line, when I try to access it. I get SISGEV</p>