Home > Back-end >  How I can implement the 2 dimensional array with different column sizes dynamically in c?
How I can implement the 2 dimensional array with different column sizes dynamically in c?

Time:11-17

How I can implement a two dimensional array with different column sizes dynamically in c?

I tried many ways to implement 2 dimensional array dynamically with different column sizes in c But I can't get it. Please tell me one suggestion...

CodePudding user response:

That type is called a jagged array. You can read about it here: enter image description here

CodePudding user response:

//Hemanth you can find the below code,
#include <stdio.h>
#include <stdlib.h> 
int main() {
   int row = 2, col = 3; //number of rows=2 and number of columns=3
   int *arr = (int *)malloc(row * col * sizeof(int)); 
   int i, j;
   for (i = 0; i < row; i  )
      for (j = 0; j < col; j  )
         *(arr   i*col   j) = i   j;    
   printf("The matrix elements are:\n");
   for (i = 0; i < row; i  ) {
      for (j = 0; j < col; j  ) {
         printf("%d ", *(arr   i*col   j)); 
      }
      printf("\n");
   }
   free(arr); 
   return 0;
}
//let me know if you are still finding any problems with it.
  • Related