I can use pointers, but I am a bit afraid of using them.

If you need a dynamic array, you can't escape pointers. Why are you afraid though? They won't bite (as long as you're careful, that is). There's no built-in dynamic array in C, you'll just have to write one yourself. In C++, you can use the built-in std::vector class. C# and just about every other high-level language also have some similar class that manages dynamic arrays for you.

If you do plan to write your own, here's something to get you started: most dynamic array implementations work by starting off with an array of some (small) default size, then whenever you run out of space when adding a new element, double the size of the array. As you can see in the example below, it's not very difficult at all: (I've omitted safety checks for brevity)

typedef struct {
  int *array;
  size_t used;
  size_t size;
} Array;

void initArray(Array *a, size_t initialSize) {
  a->array = malloc(initialSize * sizeof(int));
  a->used = 0;
  a->size = initialSize;
}

void insertArray(Array *a, int element) {
  // a->used is the number of used entries, because a->array[a->used++] updates a->used only *after* the array has been accessed.
  // Therefore a->used can go up to a->size 
  if (a->used == a->size) {
    a->size *= 2;
    a->array = realloc(a->array, a->size * sizeof(int));
  }
  a->array[a->used++] = element;
}

void freeArray(Array *a) {
  free(a->array);
  a->array = NULL;
  a->used = a->size = 0;
}

Using it is just as simple:

Array a;
int i;

initArray(&a, 5);  // initially 5 elements
for (i = 0; i < 100; i++)
  insertArray(&a, i);  // automatically resizes as necessary
printf("%d\n", a.array[9]);  // print 10th element
printf("%d\n", a.used);  // print number of elements
freeArray(&a);
Answer from casablanca on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › dynamic-array-in-c
Dynamic Array in C - GeeksforGeeks
September 22, 2026 - The elements of the array are: 1, 2, 3, 4, 5, The “calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type and initialized each block with a default value of 0...
Top answer
1 of 11
314

I can use pointers, but I am a bit afraid of using them.

If you need a dynamic array, you can't escape pointers. Why are you afraid though? They won't bite (as long as you're careful, that is). There's no built-in dynamic array in C, you'll just have to write one yourself. In C++, you can use the built-in std::vector class. C# and just about every other high-level language also have some similar class that manages dynamic arrays for you.

If you do plan to write your own, here's something to get you started: most dynamic array implementations work by starting off with an array of some (small) default size, then whenever you run out of space when adding a new element, double the size of the array. As you can see in the example below, it's not very difficult at all: (I've omitted safety checks for brevity)

typedef struct {
  int *array;
  size_t used;
  size_t size;
} Array;

void initArray(Array *a, size_t initialSize) {
  a->array = malloc(initialSize * sizeof(int));
  a->used = 0;
  a->size = initialSize;
}

void insertArray(Array *a, int element) {
  // a->used is the number of used entries, because a->array[a->used++] updates a->used only *after* the array has been accessed.
  // Therefore a->used can go up to a->size 
  if (a->used == a->size) {
    a->size *= 2;
    a->array = realloc(a->array, a->size * sizeof(int));
  }
  a->array[a->used++] = element;
}

void freeArray(Array *a) {
  free(a->array);
  a->array = NULL;
  a->used = a->size = 0;
}

Using it is just as simple:

Array a;
int i;

initArray(&a, 5);  // initially 5 elements
for (i = 0; i < 100; i++)
  insertArray(&a, i);  // automatically resizes as necessary
printf("%d\n", a.array[9]);  // print 10th element
printf("%d\n", a.used);  // print number of elements
freeArray(&a);
2 of 11
33

One simple solution involves mmap. This is great if you can tolerate a POSIX solution. Just map a whole page and guard against overflows, since realloc would fail for such values anyway. Modern OSes won't commit to the whole lot until you use it, and you can truncate files if you want.

Alternatively, there's realloc. As with everything that seems scarier at first than it was later, the best way to get over the initial fear is to immerse yourself into the discomfort of the unknown! It is at times like that which we learn the most, after all.

Unfortunately, there are limitations. While you're still learning to use a function, you shouldn't assume the role of a teacher, for example. I often read answers from those who seemingly don't know how to use realloc (i.e. the currently accepted answer!) telling others how to use it incorrectly, occasionally under the guise that they've omitted error handling, even though this is a common pitfall which needs mention. Here's an answer explaining how to use realloc correctly. Take note that the answer is storing the return value into a different variable in order to perform error checking.

Every time you call a function, and every time you use an array, you are using a pointer. The conversions are occurring implicitly, which if anything should be even scarier, as it's the things we don't see which often cause the most problems. For example, memory leaks...

Array operators are pointer operators. array[x] is really a shortcut for *(array + x), which can be broken down into: * and (array + x). It's most likely that the * is what confuses you. We can further eliminate the addition from the problem by assuming x to be 0, thus, array[0] becomes *array because adding 0 won't change the value...

... and thus we can see that *array is equivalent to array[0]. You can use one where you want to use the other, and vice versa. Array operators are pointer operators.

malloc, realloc and friends don't invent the concept of a pointer which you've been using all along; they merely use this to implement some other feature, which is a different form of storage duration, most suitable when you desire drastic, dynamic changes in size.

It is a shame that the currently accepted answer also goes against the grain of some other very well-founded advice on StackOverflow, and at the same time, misses an opportunity to introduce a little-known feature which shines for exactly this usecase: flexible array members! That's actually a pretty broken answer... :(

When you define your struct, declare your array at the end of the structure, without any upper bound. For example:

struct int_list {
    size_t size;
    int value[];
};

This will allow you to unite your array of int into the same allocation as your count, and having them bound like this can be very handy!

sizeof (struct int_list) will act as though value has a size of 0, so it'll tell you the size of the structure with an empty list. You still need to add to the size passed to realloc to specify the size of your list.

Another handy tip is to remember that realloc(NULL, x) is equivalent to malloc(x), and we can use this to simplify our code. For example:

int push_back(struct int_list **fubar, int value) {
    size_t x = *fubar ? fubar[0]->size : 0
         , y = x + 1;

    if ((x & y) == 0) {
        void *temp = realloc(*fubar, sizeof **fubar
                                   + (x + y) * sizeof fubar[0]->value[0]);
        if (!temp) { return 1; }
        *fubar = temp; // or, if you like, `fubar[0] = temp;`
    }

    fubar[0]->value[x] = value;
    fubar[0]->size = y;
    return 0;
}

struct int_list *array = NULL;

The reason I chose to use struct int_list ** as the first argument may not seem immediately obvious, but if you think about the second argument, any changes made to value from within push_back would not be visible to the function we're calling from, right? The same goes for the first argument, and we need to be able to modify our array, not just here but possibly also in any other function/s we pass it to...

array starts off pointing at nothing; it is an empty list. Initialising it is the same as adding to it. For example:

struct int_list *array = NULL;
if (!push_back(&array, 42)) {
    // success!
}

P.S. Remember to free(array); when you're done with it!

Discussions

How common are dynamic arrays in C?
Extremely common in not just C but all languages. C doesn’t have a built in dynamic array, so a lot of people roll their own. Other languages have it built in, like C#’s and Python’s List, C++’s std::vector, Java’s and Zig’s ArrayList, on and on. It’s a common data structure and language construct for a reason. If you don’t know size ahead of time, dynamic array can be a great choice. More on reddit.com
🌐 r/C_Programming
47
56
February 1, 2025
Create a dynamic array within a class (C++) - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'd like to implement a class with an array. If the size of the array is being breached, I would call a resize() function. However I can't seem to code a dynamic array as a data member. More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
Create a dynamic array of class objects inside a class in C++ - Stack Overflow
I am having trouble creating a dynamic array of class objects inside a class in C++. The concept is: assume we have a class A. In this class, I want to have an array of objects with the same type of More on stackoverflow.com
🌐 stackoverflow.com
How do you create a private dynamic array in a c++ class? - Stack Overflow
Hello I am having trouble with using dynamic arrays within a class. I am instructed that "the class VectorDouble will have a private member variable for a dynamic array of doubles." I am only as fa... More on stackoverflow.com
🌐 stackoverflow.com
November 21, 2012
🌐
Codecademy
codecademy.com › docs › arrays › dynamic arrays
C | Arrays | Dynamic Arrays | Codecademy
January 30, 2025 - A dynamic array in C refers to an array whose size can be adjusted during runtime.
🌐
Medium
medium.com › @jasonjc › making-a-dynamic-array-in-c-part-i-945e51482f2f
Making a Dynamic Array in C (Part I) | Jason J. C. | Medium
December 10, 2025 - The closest thing to simulating classes as one would have seen in Java or C++ is to create a ‘type-defining structure.’ Since we would really like a class-like container to store our little array data and stuff like that, we need to make a header file to declare and describe our implementations, and write: #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <stdint.h> typedef struct dynamicArray { int64_t *values; } DynamicArray; #endif // DYNAMICARRAY_H
🌐
Medium
medium.com › @sohaib.arshid101 › dynamic-arrays-in-c-an-implementation-guide-4a959de94332
Dynamic arrays in C: An implementation guide | by curious&dumb | Medium
March 5, 2024 - Dynamic arrays in C: An implementation guide While pursuing my gamedev journey , I stumbled upon a lecture on Pikuma where Gustavo uses an implementation of dynamic arrays for the purpose of creating …
🌐
Medium
medium.com › @anthonybaxter819 › dynamic-array-in-c-a827b9cd56ef
Dynamic Arrays in C. Where Flexibility Meets Performance | by Anthony Baxter | Medium
July 9, 2025 - Capacity: the total number of “boxes” allocated to the array, including both filled and empty ones. Element Size: the byte size of the values that will be stored. (Discussed more in depth later) When a new item is added to a dynamic array, the array first checks whether there’s enough space by comparing the length to the capacity.
🌐
Medium
medium.com › @nick-stambaugh › dynamic-arrays-in-c-fdc8a2f66b53
Dynamic Arrays in C. Understanding array manipulation is… | by Nick Stambaugh | Medium
May 4, 2024 - Dynamic Arrays in C Understanding array manipulation is fundamental for software engineers. A dynamic array is an array that can automatically resize itself when its capacity is exceeded. This is …
Find elsewhere
Top answer
1 of 4
7

Use std::vector. C++ doesn't support variable-length or dynamically-sized arrays, in classes or otherwise.

2 of 4
4

Use a std::vector and let it manage everything for you:

#include <vector>

class ArrayList
{
public:
    ArrayList()
    {
        array.reserve(ARR_SIZE);
    }

    void insert(int place, int value)
    {
        if ((place < 0) || (place > array.size()))
            std::cout << "Sorry, that index is not accessible" << std::endl;
        else
            array.insert(array.begin()+place, value);
    }

    void remove(int place)
    {
        if ((place < 0) || (place >= array.size()))
            std::cout << "Sorry, that index is not accessible" << std::endl;
        else
            array.erase(array.begin()+place);
    }

    int usedSize()
    {
        return array.size();
    }

    int totalSize()
    {
        return array.capacity();
    }

private:
    std::vector<int> array;
};

If you really want to manage the array memory yourself, you are doing it all wrong. Try this instead:

class ArrayList
{
public:
    ArrayList()
    {
        array = new int[ARR_SIZE];
        space = 0;
        size = ARR_SIZE;
    }

    ArrayList(const ArrayList &src)
    {
        array = new int[src.size];
        space = src.space;
        size = src.size;

        for(int i = 0; i < space; ++i)
            array[i] = src.array[i]; 
    }

    ~ArrayList()
    {
        delete[] array;
    }

    void insert(int place, int value)
    {
        if ((place < 0) || (place > space))
            std::cout << "Sorry, that index is not accessible" << std::endl;
        else
        {
            if (space == size)
                allocate();

            for(int i = space-1; i > place; --i)
                array[i] = array[i-1];

            array[place] = value;
            ++space;
        }
    }

    void remove(int place)
    {
        if ((place < 0) || (place >= space))
            std::cout << "Sorry, that index is not accessible" << std::endl;
        else
        {
            for(int i = place+1; i < space; ++i)
                array[i-1] = array[i];

            --space;
        }
    }

    void allocate()
    {
        int* newarray = new int[size*2];

        for (int i = 0; i < space; ++i)
            newarray[i] = array[i];

        delete[] array;
        array = newarray;

        size *= 2;
    }

    int usedSize()
    {
        return space;
    }

    int totalSize()
    {
        return size;
    }

    ArrayList& operator=(const ArrayList &src)
    {
        int *newarray = new int[src.size];

        for(int i = 0; i < src.space; ++i)
            newarray[i] = src.array[i]; 

        delete[] array;
        array = newarray;
        space = src.space;
        size = src.size;

        return *this;
    }

private:
    int *array;
    int space;
    int size;
};
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › create-dynamic-2d-array-inside-class-c
How to Create a Dynamic 2D Array Inside a Class in C++? - GeeksforGeeks
July 23, 2025 - Even if we make V static, we get the error "array bound is not an integer constant".C++ doesn't allow to creation of a stack-allocated array in a class whose size is not constant. So we need to dynamically allocate memory.
🌐
Scaler
scaler.com › home › topics › dynamic array in c
Dynamic Array in C - Scaler Topics
August 24, 2023 - They also generate much more Assembly ... post-Scaler CTC reaching ₹23L. ... Unlike other high-level languages (Python, JavaScript, etc), C doesn't have built-in dynamic arrays....
🌐
Florida State University
cs.fsu.edu › ~myers › cop3330 › notes › dma.html
Dynamic Allocation in Classes
Note that in this class, the destructor is also implemented for the Directory class, with a needed definition inside. Since the member data of an object of type Directory includes a pointer, which is being used to point to dynamically allocated space (i.e. the array of entries), it is our job in the code to deallocate that space.
🌐
Emory
cs.emory.edu › ~cheung › Courses › 255 › Syllabus › 2-C-adv-data › dyn-array.html
Dynamic arrays in C
Do-it-yourself dynamic array in C · The C programming language does not have dynamic array as a language feature
🌐
Medium
medium.com › @jcapona › dynamic-array-implementation-cpp-9deadaf1ba8e
A dynamic array implementation in C++ | by Jorge Cápona | Medium
February 15, 2017 - That’s my Array class; the important methods here are Array (constructor), push, pop, set, get and resize. The attributes used are m_size, the current size of the array, m_capacity, the current capacity of the array, andm_data, a pointer to the data of the array.
🌐
YouTube
youtube.com › watch
Creating a Dynamic Array in C: A Step-by-Step Guide - YouTube
#c Source Code:https://github.com/EricWRogers/c_vecIf you have any questions about the Vector/Dynamic ArrayCheck out the discord.My Links:Twitter: https://tw
Published: July 17, 2023
🌐
DEV Community
dev.to › pauljlucas › a-simple-dynamic-array-for-c-1k24
A Simple Dynamic Array for C - DEV Community
June 6, 2026 - The use of ARRAY_CAP_MIN having a value of 4 eliminates the expensive initial size increases (that would start out as 2, 3, and 4). The functions array_init, array_reserve, array_push_back, and array_cleanup are all you really need for a dynamic array. For convenience, you could also add functions like array_back, array_front, array_pop_back, array_qsort, and array_bsearch.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_dynamic_arrays.htm
Dynamic Arrays in C
Enter size of elements:7 Memory successfully allocated using calloc. The elements of the array are: 1, 2, 3, 4, 5, 6, 7, The realloc() function, or re-allocation, is used to dynamically change the memory size of a previously allocated block.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › dynamic array in c
Mastering Dynamic Arrays in C: Methods and Examples
May 14, 2025 - Learn how to create and manage dynamic arrays in C using malloc(), calloc(), realloc(), and more. Master flexible memory management techniques.