Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamic Memory in C

I can’t understand what doing the row

*(m[i] + sizes[i] - 1) = n;

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

#define MAXSTR 100

int main() 
  {
    int i, j, k, n;
    char str[MAXSTR];
    printf("Enter amount of rows: ");
    fgets(str, MAXSTR, stdin);
    k = atoi(str);
    int* sizes = (int * ) calloc(k, sizeof(int));
    int* sum = (int * ) calloc(k, sizeof(int));
    int** m = (int ** ) calloc(k, sizeof(int * ));
    printf("Enter matrix:\n");
    for (i = 0; i < k; i++) 
        {
          fgets(str, MAXSTR, stdin);
          char* sym = str;
          while (1) 
             {
               m[i] = (int * ) realloc(m[i], (++sizes[i]) * sizeof(int));
               n = strtol(sym, & sym, 10);
               sum[i] += n;
               if (n) 
                  {
                  *(m[i] + sizes[i] - 1) = n;
                  } 
               else 
                  {
                   --sizes[i];
                   break;
                  }
           }
       }

   printf("\nMatrix: \n");
   for (i = 0; i < k; i++) 
      {
        for (j = 0; j < sizes[i]; j++)
        printf("%i ", *(m[i] + j));
        printf("\n");
      }
    printf("\nSum of elements of row:\n");
    for (i = 0; i < k; i++)
      printf("#%i - %i\n", i + 1, sum[i]);

    free(sizes);
    free(sum);
    free(m);

    return 0;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

m is the matrix. Or more formally, it appears to be an array of "rows". Where each row is an array of integers.

sizes[i] is the length of the row represented by m[i].

This expression

*(m[i] + sizes[i] - 1) = n;

Appears to assign the value n to the last index of the row identified by m[i]. Essentially, it’s appending to the end of the reallocated array.

This entire block of code is a bit complex:

      while (1) 
         {
           m[i] = (int * ) realloc(m[i], (++sizes[i]) * sizeof(int));
           n = strtol(sym, & sym, 10);
           sum[i] += n;
           if (n) 
              {
              *(m[i] + sizes[i] - 1) = n;
              } 
           else 
              {
               --sizes[i];
               break;
              }
       }

It could be simplified to just:

int rowsize = 0;
while (1)
{
    n = strtol(sym, &sym, 10);   // parse the next number in str
    if (n == 0)                  // the row ends when 0 is read
    {
       break;
    } 
    m[i] = (int *)realloc(m[i], (rowsize+1) * sizeof(int);  // grow the row's size by 1
    m[i][rowsize] = n;
    sum[i] += n;
    rowsize++;
}
sizes[i] = rowsize;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading