How do I increase the size of an array?

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 ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    82

    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

    test[5];



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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    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

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    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%.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21