Question:
How can I declare an array in C with a variable?
jjjones42003
2009-04-16 20:03:21 UTC
How come this will declare an array

int a[10];

but this gives me a compile error?

int n = 10;
int a[n];

Is there any way to get around this? In particular, I want to make a structure that contains two fields:

int size;
int a[size];

Can this be done?
Four answers:
2009-04-16 20:32:31 UTC
Yup. In (standard) C you can't do:



int n = 10;

int a[n];



Your options are to use non-standard C (only some compilers will support this), or to do this:



int n = 10;

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

/* do stuff */

a[5] = 123;

/* do more stuff */

free(a);



This is an example of manual memory management. You call 'malloc' to allocate 10 * sizeof(int) bytes. This returns a pointer (a "place" in memory) to a block of memory that will (probably) be 40 bytes long. This is basically the description of an array as far as C is concerned so you can use it like one. It is very important that you call free when you are done using anything that you create with malloc. If you don't do this, you'll have created a bug called a "memory leak". C has no way to tell when you're done with that memory, so you have to explicitly say so.
2009-04-16 20:27:07 UTC
C99 standard supports variable-sized local arrays. In other words, you shouldn't be getting a compile-time error. Make sure that you are using the newest compiler that supports C99. See if you need to change some settings.



Otherwise, use dynamic allocation in order to solve this problem. For example:



int n = 10;

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



Now you can use a as if it were an array.



free(a); - will deallocate the memory. Search through the Internet to understand how dynamic memory allocation works.



**********************************



@feynman_rocks: You are wrong. Variable-sized arrays are a part of standard C.
?
2016-12-09 00:27:46 UTC
There are 2 styles of reminiscence i7b8b965ad4bca0e41ab51de7b31363a1 a working laptop or computer: Dy7b8b965ad4bca0e41ab51de7b31363a1amic a7b8b965ad4bca0e41ab51de7b31363a1d static. Static is greater hassle-free to get right of entry to, in spite of the undeniable fact that, it ca7b8b965ad4bca0e41ab51de7b31363a1 o7b8b965ad4bca0e41ab51de7b31363a1ly handle co7b8b965ad4bca0e41ab51de7b31363a1sta7b8b965ad4bca0e41ab51de7b31363a1t values for array sizes, so which you may the two could desire to replace n for a 7b8b965ad4bca0e41ab51de7b31363a1umber, or placed a preprocessor comma7b8b965ad4bca0e41ab51de7b31363a1d defi7b8b965ad4bca0e41ab51de7b31363a1i7b8b965ad4bca0e41ab51de7b31363a1g n as some co7b8b965ad4bca0e41ab51de7b31363a1sta7b8b965ad4bca0e41ab51de7b31363a1t "#defi7b8b965ad4bca0e41ab51de7b31363a1e n=a hundred" To get right of entry to dy7b8b965ad4bca0e41ab51de7b31363a1amic reminiscence, which accepts variables as array sizes, you may desire to i7b8b965ad4bca0e41ab51de7b31363a1itialize it as show7b8b965ad4bca0e41ab51de7b31363a1: 7b8b965ad4bca0e41ab51de7b31363a1ode *A = 7b8b965ad4bca0e41ab51de7b31363a1ew 7b8b965ad4bca0e41ab51de7b31363a1ode*[n]; or whichever variety you wa7b8b965ad4bca0e41ab51de7b31363a1t to apply replaci7b8b965ad4bca0e41ab51de7b31363a1g 7b8b965ad4bca0e41ab51de7b31363a1ode The7b8b965ad4bca0e41ab51de7b31363a1 you may desire to derefere7b8b965ad4bca0e41ab51de7b31363a1ce this poi7b8b965ad4bca0e41ab51de7b31363a1ter each and every time you desire to get right of entry to the array; the reminiscence ca7b8b965ad4bca0e41ab51de7b31363a17b8b965ad4bca0e41ab51de7b31363a1ot be moved. desire i helped :) Luke
Joran
2009-04-16 20:28:26 UTC
you can do it with pointers

int size = 10;

int *a = (int*) calloc(size,sizeof(int));

a[2] = 5;

cout<


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...