Are arrays in C static? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Are arrays in C static?

Is it possible to change the size (number of elements it can store) to x after you have declared it with the size y? How?

26th Feb 2017, 8:39 PM
infinity
infinity - avatar
2 Answers
+ 10
nope. Length is fixed during initialization. Use list if you need variable length.
26th Feb 2017, 8:54 PM
Nikolay Nachev
Nikolay Nachev - avatar
+ 5
Or you make a pointer and allocate the memory manually (void *malloc(size_t size) ). This way you can access it like a normal array. You can use sizeof(int) to get the size of a singele integer variable (or whatever you want to store) and just multiply it by the length of the array, that's how much memory you'll need. Don't forget to free() the memory afterwards though.
26th Feb 2017, 9:59 PM
Robobrine
Robobrine - avatar