I know its possible to increase the size of a dynamically allocated array.
But can I increase the size of a statically allocated array? If yes,how?
EDIT: Though this question is intended for C language, consider other languages too.Is it possible in any other language?
No. It is not. There are two options here:
Yes, that was C.
Simple answer is no, this cannot be done. Hence the name "static".
Now, lots of languages have things that look like statically allocated arrays but are actually statically allocated references to a dynamically allocated array. Those you could resize.
No. Static allocation gives the compiler permission to make all kinds of assumptions which are then baked into the program during compilation.
Among those assumptions are that:
(Well, references could be updated, if the program was stored in ram, but self-modifying programs are highly frowned upon, and surely more trouble than dynamic arrays.)
in VB .NET it would be:
not sure what langauge you're after though...
And I wouldn't use this command a lot... its terribly inefficient. Linked lists and growing data structures are much more efficient.
If you're careful, you can use
alloca()
. The array is allocated on the stack, but in terms of the code style it's a lot like if you usedmalloc
(you don't have tofree
it though, that's done automatically). I'll let you decide whether to call that a "static" array.