title: Use malloc with structure
body: <p>I'm using malloc with structures but get some errors like </p>
  
  <blockquote>
    <p>Error 1   error C2440: 'initializing' : cannot convert from 'void *' to 'my_vector *' c:\lab3\lab3\linalg.cpp 19  lab3</p>
  </blockquote>
  
  <p>I'm making MPI application and set all needed settings.
  I'm tried some solution but it doesn't helps.</p>
  
  <p>linalg.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdarg.h&gt;
  
  #include &lt;mpi.h&gt;
  
  #include "linalg.h"
  
  void fatal_error(const char *message, int errorcode)
  {
    printf("fatal error: code %d, %s\n", errorcode, message);
    fflush(stdout);
    MPI_Abort(MPI_COMM_WORLD, errorcode);
  }
  
  struct my_vector *vector_alloc(int size, double initial)
  {
    struct my_vector *result = malloc(sizeof(struct my_vector) +
                                      (size-1) * sizeof(double));
    result-&gt;size = size;
  
    for(int i = 0; i &lt; size; i++)
    {
      result-&gt;data[i] = initial;
    }
  
    return result;
  }
  
  void vector_print(FILE *f, struct my_vector *vec)
  {
    for(int i = 0; i &lt; vec-&gt;size; i++)
    {
      fprintf(f, "%.15lf ", vec-&gt;data[i]);
    }
    fprintf(f, "\n");
  }
  
  struct my_matrix *matrix_alloc(int rows, int cols, double initial)
  {
    struct my_matrix *result = malloc(sizeof(struct my_matrix) + 
                                      (rows * cols - 1) * sizeof(double));
  
    result-&gt;rows = rows;
    result-&gt;cols = cols;
  
    for(int i = 0; i &lt; rows; i++)
    {
      for(int j = 0; j &lt; cols; j++)
      {
        result-&gt;data[i * cols + j] = initial;
      }
    }
  
    return result;
  }
  
  void matrix_print(FILE *f, struct my_matrix *mat)
  {
    for(int i = 0; i &lt; mat-&gt;rows; i++)
    {
      for(int j = 0; j &lt; mat-&gt;cols; j++)
      {
        fprintf(f, "%lf ", mat-&gt;data[i * mat-&gt;cols + j]);
      }
      fprintf(f, "\n");
    }
  }
  
  struct my_matrix *read_matrix(const char *filename)
  {
    FILE *mat_file = fopen(filename, "r");
    if(mat_file == NULL)
    {
      fatal_error("can't open matrix file", 1);
    }
  
    int rows;
    int cols;
    fscanf(mat_file, "%d %d", &amp;rows, &amp;cols);
  
    struct my_matrix *result = matrix_alloc(rows, cols, 0.0);
    for(int i = 0; i &lt; rows; i++)
    {
      for(int j = 0; j &lt; cols; j++)
      {
        fscanf(mat_file, "%lf", &amp;result-&gt;data[i * cols + j]);
      }
    }
  
    fclose(mat_file);
    return result;
  }
  
  struct my_vector *read_vector(const char *filename)
  {
    FILE *vec_file = fopen(filename, "r");
    if(vec_file == NULL)
    {
      fatal_error("can't open vector file", 1);
    }
  
    int size;
    fscanf(vec_file, "%d", &amp;size);
  
    struct my_vector *result = vector_alloc(size, 0.0);
    for(int i = 0; i &lt; size; i++)
    {
      fscanf(vec_file, "%lf", &amp;result-&gt;data[i]);
    }
  
    fclose(vec_file);
    return result;
  }
  
  void write_vector(const char *filename, struct my_vector *vec)
  {
    FILE *vec_file = fopen(filename, "w");
    if(vec_file == NULL)
    {
      fatal_error("can't open vector file", 1);
    }
  
    vector_print(vec_file, vec);
  
    fclose(vec_file);
  }
  </code></pre>
  
  <p>I have problem at this place </p>
  
  <pre><code>struct my_vector *result = malloc(sizeof(struct my_vector) +
                                      (size-1) * sizeof(double));
  </code></pre>