Thread: re - declare array

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    14

    re - declare array

    For E.g i declare an array t[5]; and later in the program i want increase the array size can i declare the same array let say t[10]; again ,
    without creating a new array with a larger size and copy all of the old values into the new array.
    Can you change the size of an array ? what is dynamic memory allocation can that help ?
    how can i solve this issue?

    thanks
    Learning c
    Last edited by nevx; 07-11-2020 at 03:06 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    424
    Quote Originally Posted by nevx View Post
    For E.g i declare an array t[5]; and later in the program i want increase the array size can i declare the same array let say t[10]; again ,
    without creating a new array with a larger size and copy all of the old values into the new array.
    Can you change the size of an array ? what is dynamic memory allocation can that help ?
    how can i solve this issue?

    thanks
    Learning c

    You can't. In C, arrays declare buffers in memory. You cannot resize them. However you can achieve what you want with malloc() and realloc(). use malloc() to create a dynamic array of the initial size, then when you want to resize it, call realloc().

    malloc() allocates a chunk of memory from what we call the "heap", that is memory avialable to the program which is neither initialised at start up (globals), nor on the stack. It is used extensively in C programming, especially for large objects like images.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Sep 2019
    Posts
    14
    thanks , reading up on malloc() and realloc()

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    58
    Quote Originally Posted by Malcolm McLean View Post
    However you can achieve what you want with malloc() and realloc(). use malloc() to create a dynamic array of the initial size, then when you want to resize it, call realloc().
    You might also want to try and make the variable you use with `malloc` and `realloc` initialized to `NULL`. Then you can just use `realloc` in a loop without the initial call to `malloc`.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you declare an array in a class?
    By casablanca in forum C++ Programming
    Replies: 9
    Last Post: 08-03-2011, 12:16 PM
  2. How do I declare an array of structures?
    By Adam Rinkleff in forum C Programming
    Replies: 27
    Last Post: 06-24-2011, 10:19 PM
  3. Replies: 9
    Last Post: 02-12-2011, 10:50 AM
  4. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 05:12 PM
  5. How to declare this array?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:51 PM

Tags for this Thread