0

I want the user to decide the size of my array
Can i do it?
I tried:

#define max 8  
int a[max]; 

But then, user cant change a constant
I tried:

int i, a[i];  

But it gives an error
Help?

Daniel
  • 198
  • 2
  • 14

4 Answers4

5

Assuming your compiler supports C99 and variable-length arrays, then the following code will work:

int size;
fputs("Array size? ", stdout);
scanf("%d", &size);
int array[size];

If your compiler does not, you'll have to use malloc.

int size;
int* array;
fputs("Array size? ", stdout);
scanf("%d", &size);
array = malloc(size * sizeof(*array));
/* do stuff with array */
free(array); /* don't forget to free() when finished */

Some implementations support alloca, which allocates on the stack like a variable-length array would, but this is non-standard.

PC Luddite
  • 5,883
  • 6
  • 23
  • 39
  • To be more correct: `array=(int *) malloc(size*sizeof(int));`.. you can read about it here: http://www.cplusplus.com/forum/articles/416/ – Paulo Sep 03 '15 at 11:36
  • 1
    @Paulinho That is more wrong: you [don't cast the result of `malloc`](http://stackoverflow.com/q/605845/183120). Do you use the type or the pointee is a matter of taste, but this isn't. – legends2k Sep 03 '15 at 11:56
  • It's wrong you said it's wrong because it's not a rule that you mustn't cast the result of malloc(). You can cast, but you don't need to do it. I've just tested now and it compiled and run properly (with or without casting). – Paulo Sep 03 '15 at 12:35
  • @Paulinho The vast majority of C programmers will disagree with you. The cast is required in C++, but you shouldn't do it in C. Stop linking C++ websites. C++ is not C. They are separate languages. – PC Luddite Sep 03 '15 at 19:02
  • @PCLuddite I'll consider what you said. I'm gonna search about it. Thanks for the tip. – Paulo Sep 03 '15 at 20:15
  • i tried to use a variable length array, but it didnt work. i use VS 2013, and thought it would support C99.. i guess not?.. which one does?.. – Daniel Sep 07 '15 at 23:54
  • @daniel Unfortunately, I don't think VS 2013 supports C99. It focuses more on C++. – PC Luddite Sep 08 '15 at 03:27
4

You need to define the array after you ask the user for input.

int size;
printf("Please enter the size of the array");
scanf(" %d", &size);
int array[size];
asdf
  • 2,927
  • 2
  • 21
  • 42
3

As mentioned in above malloc is best way as it won't depend on compiler. Also because realloc can be done when you find that u need to increase of decrease the size of array.

int size;
printf("What's the array size?\n");
scanf("%d", &size);
array = (int *)malloc(size * sizeof(*array));
.
. 
. 
.some stuff on array
array = (int *)realloc(array, 1024); // changing size of array according to user.

more details regarding realloc is here resizing buffer using realloc

Community
  • 1
  • 1
Sandeep Kumar
  • 336
  • 1
  • 3
  • 9
2

For a clean solution, use malloc() function, instead of int array[size];

int size;
printf("What's the array size?\n");
scanf("%d", &size);
array=(int *) malloc(size*sizeof(int));
Paulo
  • 1,458
  • 2
  • 12
  • 26
  • 2
    How could using `malloc` ever be cleaner than a variable length array? – PC Luddite Sep 03 '15 at 02:59
  • Because `int array[size]` is not the C standard. It won't work on every single system. You can read about here: http://www.cplusplus.com/forum/articles/416/ "Assume you have variable size as a variable (it is NOT defined as constant). The compiler will give you an error if you try this: `int array[size];`" – Paulo Sep 03 '15 at 11:34
  • No, variable-length arrays *are* standard, and have been since C99. Most C compilers support them. `int array[size]` will not give you an error if you are using a **C** compiler. – PC Luddite Sep 03 '15 at 19:09
  • My mistake: C99 standard supports it. ;) – Paulo Sep 03 '15 at 20:10