Skip to content

Instantly share code, notes, and snippets.

@Kinjalrk2k
Created December 13, 2019 18:03
Show Gist options
  • Save Kinjalrk2k/357297595fa0952fc3c0c4198eb2c411 to your computer and use it in GitHub Desktop.
Save Kinjalrk2k/357297595fa0952fc3c0c4198eb2c411 to your computer and use it in GitHub Desktop.
Dynamic Array
#include <stdio.h>
#include <malloc.h>
int main(int argc, char const *argv[])
{
int n, *len_list, **twoD_arr, *oneD_arr;
printf("Enter n: ");
scanf("%d", &n);
len_list = (int *)calloc(n, sizeof(int));
twoD_arr = (int **)malloc(n * sizeof(int));
for(int i=0; i<n; i++){
printf("\nEnter size of the 1D array: ");
scanf("%d", &len_list[i]);
oneD_arr = (int *)malloc(len_list[i] * sizeof(int));
for(int j=0; j<len_list[i]; j++){
printf("arr[%d]: ", j);
scanf("%d", &oneD_arr[j]);
}
twoD_arr[i] = oneD_arr;
}
printf("\nThe combined 2D array:\n");
for(int i=0; i<n; i++){
for(int j=0; j<len_list[i]; j++)
printf("%d ", twoD_arr[i][j]);
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment