Thread: How do I increase the size of an array?

  1. #1
    Registered User
    Join Date
    Jun 2012

    How do I increase the size of an array?

    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


    So, are there any ways that I can do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Cannot be done. What you can do is to use dynamic memory allocation with say, malloc, instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    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()
    #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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Hmm...Sad...Thanks for the help anyway. I guess I will just increase the size limit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-25-2012, 06:46 AM
  2. Increase Message Queue Size
    By Yarin in forum Windows Programming
    Replies: 3
    Last Post: 12-15-2008, 10:06 AM
  3. Increase size of global multidimensional array
    By 3saul in forum C Programming
    Replies: 4
    Last Post: 04-22-2006, 09:00 PM
  4. increase array size?
    By rodrigorules in forum C Programming
    Replies: 3
    Last Post: 09-18-2005, 12:15 PM
  5. Increase Font Size Or Bold???
    By stickman in forum C++ Programming
    Replies: 10
    Last Post: 08-27-2004, 05:26 PM