Dynamic memory Allocation to static array in c

Code Snippets 4 U

In C static dynamic arrays are not allowed at stack. If we try then the error would be :

storage size of ‘arrT’ isn’t constant

So we can define an array with dynamic size at heap as follows :

static int *arrT;
arrT = malloc(n * sizeof(int));

The default value will not be 0. It will have garbage values. So, There is no benefit of doing it.

Note:

static int *arrT =  malloc(n * sizeof(int));

It will throw an error. And I don’t know why. My compiler was gcc 5.4.0

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty three + = 26