Thread: Can I create a dynamic array in C language?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    Can I create a dynamic array in C language?

    I think that no, because it is not possible to extend already defined array's length. EG if I define

    Code:
    int t[10];
    and later I need 11 numbers in the array, I would have to create a new one:

    Code:
    int b[11];
    and copy the previous 10 values from a[] to b[].

    But today on a different forum I have read that dynamic arrays are possible in C.

    So which one is true?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    "Dynamic Memory Allocation": is achieved in C by the use of malloc(), calloc(), realloc(), and free(). You will need to include "stdlib.h" to use the functions.

    You should study a good book on the C Programming Language to understand how to use these functions.

    "Variable-length automatic arrays are allowed in ISO C99..." but this doesn't fit the example you give. Again, you need to study your compiler documentation to see if it supports the C99 Standard, and for more information on how to use them.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You can't make a self-resizing array in C. C++ has that feature, in the form of std::vector. You can have "dynamic" arrays in C, starting with C99. But they aren't dynamic in the way you're thinking. They simply have a size that is defined at runtime, rather than at compile time.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User Bogdokhan's Avatar
    Join Date
    Jan 2016
    Location
    Belgium
    Posts
    8
    Non-dynamic array
    Code:
    int x[10]
    You must know the size by compiling

    pseudo-dynamic array
    Code:
    int *x = malloc(sizeof(int) * y)
    You must know the size by running (y must be defined somewhere)

    Dynamic "object" : the chained list. Read about structures and pointers to structures. There lies the answer to your question.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Quote Originally Posted by Elkvis View Post
    ... But they aren't dynamic in the way you're thinking ...
    Wikipedia says:

    Quote Originally Posted by Wikipedia
    In computer science, a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern mainstream programming languages.
    more: https://en.wikipedia.org/wiki/Dynamic_array

    I know how to add one element to an array in Javascript, there it is simple. ( JavaScript Array push() Method )

    @rstanley : malloc(), calloc(), realloc(), and free() is still an issue to discover in C for me. How far can that help me to create a dynamic array in C, that meets the definition that I have mentioned in the quote from wiki. Is it really dynamic or just meeting some part of the wikipedia definition?
    Last edited by nerio; 01-19-2016 at 02:11 PM.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by nerio View Post
    Wikipedia says:



    more: https://en.wikipedia.org/wiki/Dynamic_array

    I know how to add one element to an array in Javascript, there it is simple. ( JavaScript Array push() Method )

    @rstanley : malloc(), calloc(), realloc(), and free() is still an issue to discover in C. How far can that help me to create a dynamic array in C, that meets the definition that I have mentioned in the quote from wiki. Is it really dynamic or just meeting some part of the wikipedia definition?
    Without writing the code for you:

    Use malloc() to allocate an array of at least 10 elements in the Heap (Heap: Memory space allocated to the program, but not yet in use.) You should allocate space for additional elements if you think you will need to expand later. An array of 10 elements is pretty minimal.

    If you do need to expand the array later to hold more data, then use realloc().

    When you are finished with the data space, then call free() to return the allocated memory space to the heap to potentially be reused by some other part of the program.

    Also, please make sure you check the return values from malloc() and realloc() to insure memory was properly allocated. realloc() may relocate the address of the array if needed!

    Again, you REALLY, REALLY need to study the appropriate section of a good book to learn more about how to use this feature, and other features of the C language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create DLL file in C language and call from C and VB
    By karthickbabu in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 07:50 AM
  2. Create some animation using C language??
    By neo_lam in forum C Programming
    Replies: 3
    Last Post: 11-21-2006, 07:20 AM
  3. how to create a Dynamic Array storing objects?
    By simjay in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2006, 04:39 PM
  4. Create Dynamic Array of Unions
    By Duncan Booth in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2002, 11:58 AM