I want to increase my array's size, which is declared as 3, to a size of 5.
example :
int test[3];
I want to increase the array test size to 5
test[5];
So, are there any ways that I can do this?
This is a discussion on How do I increase the size of an array? within the C Programming forums, part of the General Programming Boards category; I want to increase my array's size, which is declared as 3, to a size of 5. example : int ...
I want to increase my array's size, which is declared as 3, to a size of 5.
example :
int test[3];
I want to increase the array test size to 5
test[5];
So, are there any ways that I can do this?
Cannot be done. What you can do is to use dynamic memory allocation with say, malloc, instead.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
You haven't provided enough information on what you are trying to achieve.
The simplest approach is to do what you've done: change the 3 to a 5 and recompile.
If you want the size to be changeable at run time (eg the size is input by the user) use malloc()
Code:#include <stdio.h> #include <stdlib.h> /* declares malloc() and free() */ int main() { int i, size; int *test; /* note this is a pointer, not an array */ printf("Enter size : "); scanf("%d", &size); test = malloc(size*sizeof(*test)); /* dynamically allocate memory for size ints */ /* Use test as if it is an array */ for (i = 0; i < size; ++i) test[i] = i; /* other stuff using test as an array */ for (i = 0; i < size; ++i) printf("%d\n", test[i]); free(test); /* when we no longer need the memory supplied by malloc(), remember to free() it */ return 0; }
Right 98% of the time, and don't care about the other 3%.
Hmm...Sad...Thanks for the help anyway. I guess I will just increase the size limit.