title: Segmentation Fault when access to a pointer of struct in struct
body: <p>I have an issue with pointers to struct that have members that are pointers to struct too.</p>
  
  <p>Browsing the suggested similar questions I found out this:</p>
  
  <p><a href="https://stackoverflow.com/questions/39252543/accessing-elements-within-a-pointer-of-a-struct-inside-another-pointer-to-a-struct">Accessing elements within a pointer of a struct inside another pointer to a struct</a></p>
  
  <p>where people suggest to pay attention to allocation memory of the structure.</p>
  
  <p>I think this is done correctly in my code.</p>
  
  <pre><code>typedef struct {
    int id_vec;
    float *vec_value;
  } Vector;
  
  typedef struct cluster{
    int id_cluster;
    float *centroid;
    Vector *patternInCluster; 
  } Cluster;
  
  int main(void){
    Cluster *cluster_ptr= malloc(3 * sizeof(Cluster));
    if (cluster_ptr==NULL){
      printf("NULL");
    }
    cluster_ptr-&gt;patternInCluster=malloc(2 * sizeof(Vector *));
    if (cluster_ptr-&gt;patternInCluster==NULL){
      printf("NULL");
      cluster_ptr-&gt;patternInCluster=NULL;
    }
  
    float p1[3]={0.0f,1.0f,2.0f};
    Vector *somePattern=malloc(2 * sizeof(Vector));
    somePattern[0].id_vec=1;
    somePattern[0].vec_value=p1;
    somePattern[1].id_vec=2;
    somePattern[1].vec_value=p1;
  }
  </code></pre>
  
  <p>Then I expect that this statement works:</p>
  
  <p><code>cluster_ptr[1].patternInCluster[1]=somePattern[1];</code></p>
  
  <p>But it compiles and produces a Segmentation Fault.</p>
  
  <p>Unexpectedly, the following statement doesn't get errors:</p>
  
  <p><code>cluster_ptr[0].patternInCluster[1]=somePattern[1];</code></p>
  
  <p>and a test show me correct result(somePattern[1] id and value)</p>
  
  <p>I tried to debug with gdb but I only can see this:</p>
  
  <p><code>Program received signal SIGSEGV, Segmentation fault.
  0x00005555555547fe in main () at test_struct.c:36
  36      cluster_ptr[1].patternInCluster[1]=somePattern[1];</code></p>
  
  <p>Am I missing some allocation mistakes?</p>