c - How can I allocate a 2D array using double pointers? -
i want know how can form 2d array using double pointers?
suppose array declaration is:
char array[100][100];
how can double pointer has same allocation , properties?
typical procedure dynamically allocating 2d array using pointer-to-pointer::
#include <stdlib.h> ... t **arr; // type t arr = malloc(sizeof *arr * rows); if (arr) { size_t i; (i = 0; < rows; i++) { arr[i] = malloc(sizeof *arr[i] * cols); if (arr[i]) // initialize arr[i] else // panic } }
note since you're allocating each row separately, contents of array may not contiguous.
Comments
Post a Comment