how to create an array of n 0s in C code example

Example 1: create n number of arrray in c

int n,i;

//enter n

int **array = malloc(sizeof(int*)*n);

for(i=0;i<n;i++)
  array[i] = malloc(sizeof(int)*64);

 /* Do Stuffs*/


/* Free Memory */  
for(i=0;i<n;i++)
  free(array[i]);

free(array);

Example 2: initialize array in c with 0

static char ZEROARRAY[1024]; // if declaration is in global scope or is static it will alredy be initialized to Zeroes
// OR
char ZEROARRAY[1024] = {0}; // Compiler fills unwritten entries with zeroes
// OR
memset(ZEROARRAY, 0, 1024); // Alternatively you could use memset to initialize the array at program startup:

Tags:

Cpp Example