Initialize matrix (double pointer) member of struct












2















I'm having a rough time with pointers, can someone help me a little bit?



I'm trying to initialize a double (double) pointer inside a struct array, but I'm somehow doing it wrong.. Example:



struct MyStruct
{
double **matrix;
};

double **CalculateMatrix(...)
{
double **matrix = (double**)malloc(ROWS * sizeof(double*));
for (int i = 0; i < ROWS; i++)
matrix[i] = (double*)malloc(COLS * sizeof(double));
// + assign some values
return matrix;
}

void Initialize(struct MyStruct *structs, int size)
{
for (int i = 0; i < size; i++)
// Here structs[i].matrix holds the correct matrix values
structs[i].matrix = CalculateMatrix(...);
}

int main()
{
struct MyStruct *structs = (struct MyStruct*)malloc(SIZE * sizeof(struct MyStruct));
Initialize(structs, SIZE);
// but once the function returns, matrix values are completely different
}


Sorry if it's duplicated, I couldn't find anything










share|improve this question




















  • 1





    did you forgot to pass size to Initialize(structs);? It should give compilation error or warning.

    – kiran Biradar
    Nov 21 '18 at 9:08








  • 1





    The code you show looks fine (assuming you pass the arguments you're supposed to pass), so the problem is probably in the code you don't show. Try to use a memory debugger such a Valgrind (or similar) to find out if you're having some problems with out-of-bounds access or similar.

    – Some programmer dude
    Nov 21 '18 at 9:09








  • 1





    Oh and in C you should not cast the result of malloc.

    – Some programmer dude
    Nov 21 '18 at 9:10











  • Yeah, forgot to add the size variable, it's just an example anyways. The only real difference with the real code it's that the structs defined inside main it's a static global variable, does that change anything?

    – Borja Sanchidrián Monge
    Nov 21 '18 at 9:11











  • And please read about how to ask good questions, as well as this question checklist. Lastly try to create an Minimal, Complete, and Verifiable example of the failing code to show us.

    – Some programmer dude
    Nov 21 '18 at 9:11
















2















I'm having a rough time with pointers, can someone help me a little bit?



I'm trying to initialize a double (double) pointer inside a struct array, but I'm somehow doing it wrong.. Example:



struct MyStruct
{
double **matrix;
};

double **CalculateMatrix(...)
{
double **matrix = (double**)malloc(ROWS * sizeof(double*));
for (int i = 0; i < ROWS; i++)
matrix[i] = (double*)malloc(COLS * sizeof(double));
// + assign some values
return matrix;
}

void Initialize(struct MyStruct *structs, int size)
{
for (int i = 0; i < size; i++)
// Here structs[i].matrix holds the correct matrix values
structs[i].matrix = CalculateMatrix(...);
}

int main()
{
struct MyStruct *structs = (struct MyStruct*)malloc(SIZE * sizeof(struct MyStruct));
Initialize(structs, SIZE);
// but once the function returns, matrix values are completely different
}


Sorry if it's duplicated, I couldn't find anything










share|improve this question




















  • 1





    did you forgot to pass size to Initialize(structs);? It should give compilation error or warning.

    – kiran Biradar
    Nov 21 '18 at 9:08








  • 1





    The code you show looks fine (assuming you pass the arguments you're supposed to pass), so the problem is probably in the code you don't show. Try to use a memory debugger such a Valgrind (or similar) to find out if you're having some problems with out-of-bounds access or similar.

    – Some programmer dude
    Nov 21 '18 at 9:09








  • 1





    Oh and in C you should not cast the result of malloc.

    – Some programmer dude
    Nov 21 '18 at 9:10











  • Yeah, forgot to add the size variable, it's just an example anyways. The only real difference with the real code it's that the structs defined inside main it's a static global variable, does that change anything?

    – Borja Sanchidrián Monge
    Nov 21 '18 at 9:11











  • And please read about how to ask good questions, as well as this question checklist. Lastly try to create an Minimal, Complete, and Verifiable example of the failing code to show us.

    – Some programmer dude
    Nov 21 '18 at 9:11














2












2








2








I'm having a rough time with pointers, can someone help me a little bit?



I'm trying to initialize a double (double) pointer inside a struct array, but I'm somehow doing it wrong.. Example:



struct MyStruct
{
double **matrix;
};

double **CalculateMatrix(...)
{
double **matrix = (double**)malloc(ROWS * sizeof(double*));
for (int i = 0; i < ROWS; i++)
matrix[i] = (double*)malloc(COLS * sizeof(double));
// + assign some values
return matrix;
}

void Initialize(struct MyStruct *structs, int size)
{
for (int i = 0; i < size; i++)
// Here structs[i].matrix holds the correct matrix values
structs[i].matrix = CalculateMatrix(...);
}

int main()
{
struct MyStruct *structs = (struct MyStruct*)malloc(SIZE * sizeof(struct MyStruct));
Initialize(structs, SIZE);
// but once the function returns, matrix values are completely different
}


Sorry if it's duplicated, I couldn't find anything










share|improve this question
















I'm having a rough time with pointers, can someone help me a little bit?



I'm trying to initialize a double (double) pointer inside a struct array, but I'm somehow doing it wrong.. Example:



struct MyStruct
{
double **matrix;
};

double **CalculateMatrix(...)
{
double **matrix = (double**)malloc(ROWS * sizeof(double*));
for (int i = 0; i < ROWS; i++)
matrix[i] = (double*)malloc(COLS * sizeof(double));
// + assign some values
return matrix;
}

void Initialize(struct MyStruct *structs, int size)
{
for (int i = 0; i < size; i++)
// Here structs[i].matrix holds the correct matrix values
structs[i].matrix = CalculateMatrix(...);
}

int main()
{
struct MyStruct *structs = (struct MyStruct*)malloc(SIZE * sizeof(struct MyStruct));
Initialize(structs, SIZE);
// but once the function returns, matrix values are completely different
}


Sorry if it's duplicated, I couldn't find anything







c pointers matrix memory-management struct






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:31









gsamaras

51.8k24105191




51.8k24105191










asked Nov 21 '18 at 9:06









Borja Sanchidrián MongeBorja Sanchidrián Monge

286




286








  • 1





    did you forgot to pass size to Initialize(structs);? It should give compilation error or warning.

    – kiran Biradar
    Nov 21 '18 at 9:08








  • 1





    The code you show looks fine (assuming you pass the arguments you're supposed to pass), so the problem is probably in the code you don't show. Try to use a memory debugger such a Valgrind (or similar) to find out if you're having some problems with out-of-bounds access or similar.

    – Some programmer dude
    Nov 21 '18 at 9:09








  • 1





    Oh and in C you should not cast the result of malloc.

    – Some programmer dude
    Nov 21 '18 at 9:10











  • Yeah, forgot to add the size variable, it's just an example anyways. The only real difference with the real code it's that the structs defined inside main it's a static global variable, does that change anything?

    – Borja Sanchidrián Monge
    Nov 21 '18 at 9:11











  • And please read about how to ask good questions, as well as this question checklist. Lastly try to create an Minimal, Complete, and Verifiable example of the failing code to show us.

    – Some programmer dude
    Nov 21 '18 at 9:11














  • 1





    did you forgot to pass size to Initialize(structs);? It should give compilation error or warning.

    – kiran Biradar
    Nov 21 '18 at 9:08








  • 1





    The code you show looks fine (assuming you pass the arguments you're supposed to pass), so the problem is probably in the code you don't show. Try to use a memory debugger such a Valgrind (or similar) to find out if you're having some problems with out-of-bounds access or similar.

    – Some programmer dude
    Nov 21 '18 at 9:09








  • 1





    Oh and in C you should not cast the result of malloc.

    – Some programmer dude
    Nov 21 '18 at 9:10











  • Yeah, forgot to add the size variable, it's just an example anyways. The only real difference with the real code it's that the structs defined inside main it's a static global variable, does that change anything?

    – Borja Sanchidrián Monge
    Nov 21 '18 at 9:11











  • And please read about how to ask good questions, as well as this question checklist. Lastly try to create an Minimal, Complete, and Verifiable example of the failing code to show us.

    – Some programmer dude
    Nov 21 '18 at 9:11








1




1





did you forgot to pass size to Initialize(structs);? It should give compilation error or warning.

– kiran Biradar
Nov 21 '18 at 9:08







did you forgot to pass size to Initialize(structs);? It should give compilation error or warning.

– kiran Biradar
Nov 21 '18 at 9:08






1




1





The code you show looks fine (assuming you pass the arguments you're supposed to pass), so the problem is probably in the code you don't show. Try to use a memory debugger such a Valgrind (or similar) to find out if you're having some problems with out-of-bounds access or similar.

– Some programmer dude
Nov 21 '18 at 9:09







The code you show looks fine (assuming you pass the arguments you're supposed to pass), so the problem is probably in the code you don't show. Try to use a memory debugger such a Valgrind (or similar) to find out if you're having some problems with out-of-bounds access or similar.

– Some programmer dude
Nov 21 '18 at 9:09






1




1





Oh and in C you should not cast the result of malloc.

– Some programmer dude
Nov 21 '18 at 9:10





Oh and in C you should not cast the result of malloc.

– Some programmer dude
Nov 21 '18 at 9:10













Yeah, forgot to add the size variable, it's just an example anyways. The only real difference with the real code it's that the structs defined inside main it's a static global variable, does that change anything?

– Borja Sanchidrián Monge
Nov 21 '18 at 9:11





Yeah, forgot to add the size variable, it's just an example anyways. The only real difference with the real code it's that the structs defined inside main it's a static global variable, does that change anything?

– Borja Sanchidrián Monge
Nov 21 '18 at 9:11













And please read about how to ask good questions, as well as this question checklist. Lastly try to create an Minimal, Complete, and Verifiable example of the failing code to show us.

– Some programmer dude
Nov 21 '18 at 9:11





And please read about how to ask good questions, as well as this question checklist. Lastly try to create an Minimal, Complete, and Verifiable example of the failing code to show us.

– Some programmer dude
Nov 21 '18 at 9:11












2 Answers
2






active

oldest

votes


















1














You do not need global variables here. So, you could declare, define and initialize the size of your struct array, as well as the dimensions of your matrices in the main method.



Moreover, your methods' names are misleading, I changed them to something that communicates to the author what every function's purpose is. Or, more accurately your methods do more than one tasks. It's good to divide tasks for reusability.



Here is a minimal example to get you started:



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

struct MyStruct
{
double **matrix;
};

double **allocate_matrix(int rows, int cols)
{
double **matrix = malloc(rows * sizeof(double*));
for (int i = 0; i < rows; i++)
matrix[i] = malloc(cols * sizeof(double));
return matrix;
}

void allocate_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
for (int i = 0; i < size; i++)
structs[i].matrix = allocate_matrix(rows, cols);
}

void fill_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
for (int i = 0; i < size; i++)
for(int j = 0; j < rows; j++)
for(int z = 0; z < cols; z++)
structs[i].matrix[j][z] = -1.2;
}

void print_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
for (int i = 0; i < size; i++)
for(int j = 0; j < rows; j++)
for(int z = 0; z < cols; z++)
printf("%fn", structs[i].matrix[j][z]);
}

void free_matrices(struct MyStruct *structs, int size, int rows) {
for (int i = 0; i < size; i++) {
for(int j = 0; j < rows; j++) {
free(structs[i].matrix[j]);
}
free(structs[i].matrix);
}
}


int main()
{
int rows = 3, cols = 4, size = 2;
struct MyStruct *structs = malloc(size * sizeof(struct MyStruct));
structs[0].matrix = NULL;
structs[1].matrix = NULL;
if(structs[0].matrix == NULL)
printf("nulln");
allocate_matrices(structs, size, rows, cols);
if(structs[0].matrix == NULL)
printf("nulln");
fill_matrices(structs, size, rows, cols);
print_matrices(structs, size, rows, cols);
free_matrices(structs, size, rows);
free(structs);
}


Output:



null
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000


Inspired from my 2D dynamic array (C).






share|improve this answer

































    0














    Extending gsamaras' answer:



    'You do not need global variables here' is truth. Still, having constants ROWS and COLS can be meaningful as well. It depends on the use case!



    gsamaras' approach allowes you to define individual sizes for each matrix. If you want to enforce same size for all matrices created, it is less error prone not to have parameters for but constants instead.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408525%2finitialize-matrix-double-pointer-member-of-struct%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You do not need global variables here. So, you could declare, define and initialize the size of your struct array, as well as the dimensions of your matrices in the main method.



      Moreover, your methods' names are misleading, I changed them to something that communicates to the author what every function's purpose is. Or, more accurately your methods do more than one tasks. It's good to divide tasks for reusability.



      Here is a minimal example to get you started:



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

      struct MyStruct
      {
      double **matrix;
      };

      double **allocate_matrix(int rows, int cols)
      {
      double **matrix = malloc(rows * sizeof(double*));
      for (int i = 0; i < rows; i++)
      matrix[i] = malloc(cols * sizeof(double));
      return matrix;
      }

      void allocate_matrices(struct MyStruct *structs, int size, int rows, int cols)
      {
      for (int i = 0; i < size; i++)
      structs[i].matrix = allocate_matrix(rows, cols);
      }

      void fill_matrices(struct MyStruct *structs, int size, int rows, int cols)
      {
      for (int i = 0; i < size; i++)
      for(int j = 0; j < rows; j++)
      for(int z = 0; z < cols; z++)
      structs[i].matrix[j][z] = -1.2;
      }

      void print_matrices(struct MyStruct *structs, int size, int rows, int cols)
      {
      for (int i = 0; i < size; i++)
      for(int j = 0; j < rows; j++)
      for(int z = 0; z < cols; z++)
      printf("%fn", structs[i].matrix[j][z]);
      }

      void free_matrices(struct MyStruct *structs, int size, int rows) {
      for (int i = 0; i < size; i++) {
      for(int j = 0; j < rows; j++) {
      free(structs[i].matrix[j]);
      }
      free(structs[i].matrix);
      }
      }


      int main()
      {
      int rows = 3, cols = 4, size = 2;
      struct MyStruct *structs = malloc(size * sizeof(struct MyStruct));
      structs[0].matrix = NULL;
      structs[1].matrix = NULL;
      if(structs[0].matrix == NULL)
      printf("nulln");
      allocate_matrices(structs, size, rows, cols);
      if(structs[0].matrix == NULL)
      printf("nulln");
      fill_matrices(structs, size, rows, cols);
      print_matrices(structs, size, rows, cols);
      free_matrices(structs, size, rows);
      free(structs);
      }


      Output:



      null
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000
      -1.200000


      Inspired from my 2D dynamic array (C).






      share|improve this answer






























        1














        You do not need global variables here. So, you could declare, define and initialize the size of your struct array, as well as the dimensions of your matrices in the main method.



        Moreover, your methods' names are misleading, I changed them to something that communicates to the author what every function's purpose is. Or, more accurately your methods do more than one tasks. It's good to divide tasks for reusability.



        Here is a minimal example to get you started:



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

        struct MyStruct
        {
        double **matrix;
        };

        double **allocate_matrix(int rows, int cols)
        {
        double **matrix = malloc(rows * sizeof(double*));
        for (int i = 0; i < rows; i++)
        matrix[i] = malloc(cols * sizeof(double));
        return matrix;
        }

        void allocate_matrices(struct MyStruct *structs, int size, int rows, int cols)
        {
        for (int i = 0; i < size; i++)
        structs[i].matrix = allocate_matrix(rows, cols);
        }

        void fill_matrices(struct MyStruct *structs, int size, int rows, int cols)
        {
        for (int i = 0; i < size; i++)
        for(int j = 0; j < rows; j++)
        for(int z = 0; z < cols; z++)
        structs[i].matrix[j][z] = -1.2;
        }

        void print_matrices(struct MyStruct *structs, int size, int rows, int cols)
        {
        for (int i = 0; i < size; i++)
        for(int j = 0; j < rows; j++)
        for(int z = 0; z < cols; z++)
        printf("%fn", structs[i].matrix[j][z]);
        }

        void free_matrices(struct MyStruct *structs, int size, int rows) {
        for (int i = 0; i < size; i++) {
        for(int j = 0; j < rows; j++) {
        free(structs[i].matrix[j]);
        }
        free(structs[i].matrix);
        }
        }


        int main()
        {
        int rows = 3, cols = 4, size = 2;
        struct MyStruct *structs = malloc(size * sizeof(struct MyStruct));
        structs[0].matrix = NULL;
        structs[1].matrix = NULL;
        if(structs[0].matrix == NULL)
        printf("nulln");
        allocate_matrices(structs, size, rows, cols);
        if(structs[0].matrix == NULL)
        printf("nulln");
        fill_matrices(structs, size, rows, cols);
        print_matrices(structs, size, rows, cols);
        free_matrices(structs, size, rows);
        free(structs);
        }


        Output:



        null
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000
        -1.200000


        Inspired from my 2D dynamic array (C).






        share|improve this answer




























          1












          1








          1







          You do not need global variables here. So, you could declare, define and initialize the size of your struct array, as well as the dimensions of your matrices in the main method.



          Moreover, your methods' names are misleading, I changed them to something that communicates to the author what every function's purpose is. Or, more accurately your methods do more than one tasks. It's good to divide tasks for reusability.



          Here is a minimal example to get you started:



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

          struct MyStruct
          {
          double **matrix;
          };

          double **allocate_matrix(int rows, int cols)
          {
          double **matrix = malloc(rows * sizeof(double*));
          for (int i = 0; i < rows; i++)
          matrix[i] = malloc(cols * sizeof(double));
          return matrix;
          }

          void allocate_matrices(struct MyStruct *structs, int size, int rows, int cols)
          {
          for (int i = 0; i < size; i++)
          structs[i].matrix = allocate_matrix(rows, cols);
          }

          void fill_matrices(struct MyStruct *structs, int size, int rows, int cols)
          {
          for (int i = 0; i < size; i++)
          for(int j = 0; j < rows; j++)
          for(int z = 0; z < cols; z++)
          structs[i].matrix[j][z] = -1.2;
          }

          void print_matrices(struct MyStruct *structs, int size, int rows, int cols)
          {
          for (int i = 0; i < size; i++)
          for(int j = 0; j < rows; j++)
          for(int z = 0; z < cols; z++)
          printf("%fn", structs[i].matrix[j][z]);
          }

          void free_matrices(struct MyStruct *structs, int size, int rows) {
          for (int i = 0; i < size; i++) {
          for(int j = 0; j < rows; j++) {
          free(structs[i].matrix[j]);
          }
          free(structs[i].matrix);
          }
          }


          int main()
          {
          int rows = 3, cols = 4, size = 2;
          struct MyStruct *structs = malloc(size * sizeof(struct MyStruct));
          structs[0].matrix = NULL;
          structs[1].matrix = NULL;
          if(structs[0].matrix == NULL)
          printf("nulln");
          allocate_matrices(structs, size, rows, cols);
          if(structs[0].matrix == NULL)
          printf("nulln");
          fill_matrices(structs, size, rows, cols);
          print_matrices(structs, size, rows, cols);
          free_matrices(structs, size, rows);
          free(structs);
          }


          Output:



          null
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000


          Inspired from my 2D dynamic array (C).






          share|improve this answer















          You do not need global variables here. So, you could declare, define and initialize the size of your struct array, as well as the dimensions of your matrices in the main method.



          Moreover, your methods' names are misleading, I changed them to something that communicates to the author what every function's purpose is. Or, more accurately your methods do more than one tasks. It's good to divide tasks for reusability.



          Here is a minimal example to get you started:



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

          struct MyStruct
          {
          double **matrix;
          };

          double **allocate_matrix(int rows, int cols)
          {
          double **matrix = malloc(rows * sizeof(double*));
          for (int i = 0; i < rows; i++)
          matrix[i] = malloc(cols * sizeof(double));
          return matrix;
          }

          void allocate_matrices(struct MyStruct *structs, int size, int rows, int cols)
          {
          for (int i = 0; i < size; i++)
          structs[i].matrix = allocate_matrix(rows, cols);
          }

          void fill_matrices(struct MyStruct *structs, int size, int rows, int cols)
          {
          for (int i = 0; i < size; i++)
          for(int j = 0; j < rows; j++)
          for(int z = 0; z < cols; z++)
          structs[i].matrix[j][z] = -1.2;
          }

          void print_matrices(struct MyStruct *structs, int size, int rows, int cols)
          {
          for (int i = 0; i < size; i++)
          for(int j = 0; j < rows; j++)
          for(int z = 0; z < cols; z++)
          printf("%fn", structs[i].matrix[j][z]);
          }

          void free_matrices(struct MyStruct *structs, int size, int rows) {
          for (int i = 0; i < size; i++) {
          for(int j = 0; j < rows; j++) {
          free(structs[i].matrix[j]);
          }
          free(structs[i].matrix);
          }
          }


          int main()
          {
          int rows = 3, cols = 4, size = 2;
          struct MyStruct *structs = malloc(size * sizeof(struct MyStruct));
          structs[0].matrix = NULL;
          structs[1].matrix = NULL;
          if(structs[0].matrix == NULL)
          printf("nulln");
          allocate_matrices(structs, size, rows, cols);
          if(structs[0].matrix == NULL)
          printf("nulln");
          fill_matrices(structs, size, rows, cols);
          print_matrices(structs, size, rows, cols);
          free_matrices(structs, size, rows);
          free(structs);
          }


          Output:



          null
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000
          -1.200000


          Inspired from my 2D dynamic array (C).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 9:38

























          answered Nov 21 '18 at 9:30









          gsamarasgsamaras

          51.8k24105191




          51.8k24105191

























              0














              Extending gsamaras' answer:



              'You do not need global variables here' is truth. Still, having constants ROWS and COLS can be meaningful as well. It depends on the use case!



              gsamaras' approach allowes you to define individual sizes for each matrix. If you want to enforce same size for all matrices created, it is less error prone not to have parameters for but constants instead.






              share|improve this answer




























                0














                Extending gsamaras' answer:



                'You do not need global variables here' is truth. Still, having constants ROWS and COLS can be meaningful as well. It depends on the use case!



                gsamaras' approach allowes you to define individual sizes for each matrix. If you want to enforce same size for all matrices created, it is less error prone not to have parameters for but constants instead.






                share|improve this answer


























                  0












                  0








                  0







                  Extending gsamaras' answer:



                  'You do not need global variables here' is truth. Still, having constants ROWS and COLS can be meaningful as well. It depends on the use case!



                  gsamaras' approach allowes you to define individual sizes for each matrix. If you want to enforce same size for all matrices created, it is less error prone not to have parameters for but constants instead.






                  share|improve this answer













                  Extending gsamaras' answer:



                  'You do not need global variables here' is truth. Still, having constants ROWS and COLS can be meaningful as well. It depends on the use case!



                  gsamaras' approach allowes you to define individual sizes for each matrix. If you want to enforce same size for all matrices created, it is less error prone not to have parameters for but constants instead.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 10:07









                  AconcaguaAconcagua

                  12.7k32144




                  12.7k32144






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408525%2finitialize-matrix-double-pointer-member-of-struct%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?