A homebrew ArrayList would work the way Vectors do in C++ behind the scenes: arrays! (Note, this is also how Java implements ArrayLists behind the scenes as well). They maintain an array member variable with a certain amount of space. As new elements are added they manually shift items around to insert them into certain spots, or just add them at the back if that's all you need. But if they realize the member array does not have enough storage, they will create a new array (typically double the size or something like that) and then manually copy all the elements over to that new array. Because all the elements are stored in a member array, which has instant "random access," this makes index access far more efficient than it is for linked lists. However, it makes insertions into the middle of the list and deletions more inefficient - in theory. Answer from Chillbrosaurus_Rex on reddit.com
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ can we create arraylists in c where we can access an index directly?
r/C_Programming on Reddit: Can we create ArrayLists in C where we can access an index directly?
October 12, 2020 -

I know how to make a LinkedList in C but I'm struggling to understand how an ArrayList would be implemented in C - especially the addition and direct access parts

Top answer
1 of 5
23
A homebrew ArrayList would work the way Vectors do in C++ behind the scenes: arrays! (Note, this is also how Java implements ArrayLists behind the scenes as well). They maintain an array member variable with a certain amount of space. As new elements are added they manually shift items around to insert them into certain spots, or just add them at the back if that's all you need. But if they realize the member array does not have enough storage, they will create a new array (typically double the size or something like that) and then manually copy all the elements over to that new array. Because all the elements are stored in a member array, which has instant "random access," this makes index access far more efficient than it is for linked lists. However, it makes insertions into the middle of the list and deletions more inefficient - in theory.
2 of 5
4
Firstly I'd say that, while its totally educational and interesting to try implementing such things in C, I would experiment with avoiding using it for general C stuff! Vectors / Lists are structures with fast indexed access, unpredictable appending times (due to occasional re-allocation), and potentially wasteful memory footprint. Consider a simple array of up to 100 chars: char test_array[100]; With this, we can access anywhere between array[0] and array[99]. If we need more, we malloc() the required memory, copy everything over, and reassign test_array to the new memory! However, malloc() could potentially fail, or take a (relative) while... (There is also realloc() which automates some of this). So that's dynamic length handled! Hopefully you can see that it sacrifices speed and potentially memory. The C++ std::vector is often padded out at the start using resize() to make sure these sporadic malloc()s don't slow down your code... So you are back to using the maximum amount of memory anyway! Also, when std::vector does need to grow, it grows by a whole lot, hoping to avoid growing again too soon... As you can imagine, hitting a limit when you just want to append 1 entry could waste a few bytes! This is one of the many reasons embedded C++ avoids std::vector. To keep track of the used length, and the maximum capacity, we just need more variables. So behind the scenes, you could do a ArrayList of floats with: EDIT: see the replies for corrections to my dodgy untested code! Fixed for posterity :) float* array; size_t array_length; size_t array_capacity; With a little extra arithmetic, and a commitment to never exceed capacity (or realloc as you go, if you really want an application that grows in RAM footprint) you can do it all: array_capacity = 1000; array = malloc(array_capacity * sizeof(float)); array_size = 0; array[array_size] = 1.5f; array_size++; Of course this could be wrapped in functions - you could even make a generic structure using an array of bytes, and looking up the index * byte_stride. Honestly though, lists are usually parts of systems, and I prefer to let systems handle their own arrays :) As I said, theres nothing wrong with ArrayLists in themselves - heck you could be writing a high level language's backend for all I know! But if you want to understand their implementation, in the context of why C doesn't provide them, hopefully this reply was somewhat helpful :) Up front allocation, avoiding malloc after initialisation, low indirection... all these things help turn C code from chunky corporate software to efficient, Mars-rover-friendly routines :)
๐ŸŒ
Ducmanhphan
ducmanhphan.github.io โ€บ 2022-04-17-how-to-convert-int-to-list-integer
How to convert int[] to List of Integer
So we will convert the int[] to Integer[], then pass it as the argument. int[] rawData = {1, 3, 5}; List<Integer> data = new ArrayList<Integer>(); for (int num : rawData) { data.add(num); }
๐ŸŒ
GitHub
github.com โ€บ BaseMax โ€บ ArrayListC
GitHub - BaseMax/ArrayListC: This is a simple implementation of an ArrayList in C using a struct, a pointer to the struct and a pointer to the array. The array is static and the size of the array is defined when the ArrayList is created.
The array is static and the size ... is created. ArrayList* ArrayCreate(int n): Creates an ArrayList with a size of n. void ArrayAppendFirst(ArrayList* list, int value): Add an element to the beginning of the list....
Author ย  BaseMax
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_arrays.php
C Arrays
We have now created a variable that holds an array of four integers. To access an array element, refer to its index number.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-arrays
Arrays in C - GeeksforGeeks
The sizeof() operator returns the size in bytes. sizeof(arr) returns the total number of bytes of the array. In an array, each element is of type int, which is 4 bytes. Therefore, we can calculate the size of the array by dividing the total number of bytes by the byte size of one element...
Published ย  October 17, 2025
Find elsewhere
๐ŸŒ
Unity
discussions.unity.com โ€บ questions & answers
Need help with converting an array to a list in C#` - Questions & Answers - Unity Discussions
March 9, 2014 - Now since that part works, I donโ€™t want to mess around with it unless I have to, I need to convert deck information brought in from an array into a list, so itโ€™s easier to manipulate. Here is the bit of code that is given me an issue: var deck = DeckContainer.Load(Path.Combine(Applicatio...
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ c arrays: types, examples, & advantages
Array in C: Types, Examples, and Advantages Explained
July 31, 2025 - Learn key concepts of arrays in C and how to implement them for storing values. Get practical insights, code examples, and step-by-step guidance in this guide.
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ c-arrays
C Arrays (With Examples)
November 10, 2024 - In this tutorial, you will learn to work with arrays. You will learn to declare, initialize and access array elements of an array with the help of examples. An array is a variable that can store multiple values.
๐ŸŒ
McNeel Forum
discourse.mcneel.com โ€บ grasshopper
C# array to list - Grasshopper - McNeel Forum
September 14, 2020 - I do not want to make a loop to go from array to list. ToList() does not work new List<object>(myArray) does not work List<object> list = myArray.ToList<object>(); does not work ConvertAll is not clear to me Do you โ€ฆ
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 596146 โ€บ languages โ€บ ArrayList-equivalent
ArrayList equivalent in C? (C / C++ forum at Coderanch)
Hi guys, I'm making a simple calculator program in C. It's supposed to be able to save loads of (floating point) numbers in an array and the array is supposed to expand to accommodate the new numbers. As I've been doing Java for quite some time now, I know that an ArrayList would be useful in this situation, but is there something similar to it in C? Thanks! ... Not that I know of. You most likely need to define your own data structure as an array list.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 63771-array-linked-list.html
array to linked list
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem ... #include <stdio.h> struct node { int data; struct node* next; }; void insert(struct node** head, int key); void print_list(struct node* head); int main(int argc, char **argv) { struct node* list = NULL; insert(&list,5); insert(&list,7); insert(&list,2); print_list(list); return 0; } void insert(struct node** head, int key) { struct node** current = head; while (*current != NULL) { /* traverse the list */ current = &((*current)->next); } /* make new node and re-link */ struct node* new = (struct node*)
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ cprogramming โ€บ c arrays
C Arrays
June 10, 2012 - Arrays are used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. An array in C is a collection of data items of similar data type. One or more values same data type, which may be primary data types (int, float, char), ...
๐ŸŒ
Wikibooks
en.wikibooks.org โ€บ wiki โ€บ C_Programming โ€บ Arrays_and_strings
C Programming/Arrays and strings - Wikibooks, open books for an open world
August 11, 2003 - Adding an additional value to the list will cause it to be sized to six, and because of the sizeof expression in the for loop, the code automatically adjusts to this change. Good programming practice is to declare a variable size , and store the number of elements in the array in it.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how to print a full array?
r/C_Programming on Reddit: How to print a full array?
September 7, 2024 -

In python, I would use a list to store some numbers than print all of them:
x = [1, 2, 3, 4]

print(x) #output = [1, 2, 3, 4]

How should I do it in C with an array?

Another question: is an array similar to the python lists? If not, what type would be it?;

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ initialize-an-array-in-c
Initialize an Array in C | DigitalOcean
August 4, 2022 - But now, arr[4] and arr[5] will ... the array. // Valid. Size of the array is taken as the number of elements // in the initializer list (5) int arr[] = {1, 2, 3, 4, 5}; // Also valid....
Top answer
1 of 4
9

A definitely better than average effort.

No documentation

.h file deserves overall documentation. Consider users should be able to understand what these functions do without access to the .c file.

Bug: al_contains()

al_contains() returns 0 when data is not found or if data != 0.

Did OP want al_contains() to return the index when found?

Hide it

struct array_list definition not needed in .h file. Only its declaration needed. Research Information hiding

Unclear return

al_find() returns -1 when data not found, yet -1 is a valid data. Consider a different approach.

Use const

For functions that do not modify the state of the list:
Example:

//int al_is_valid_index(struct array_list *list, int index);
int al_is_valid_index(const struct array_list *list, int index);

Include first

In array_list.c,code #include "array_list.h" first to test that it does not rely on any <.h> files that it does not include itself.

Why 10?

Zero is a better choice for AL_INITIAL_CAPACITY.

Often the list are used, there are many empty ones. Zero is a natural choice.

If concerned about a lot of initial re-allocations, simply jump use to 10 when first needed.

Name space

Code uses al_... and array_list.... Use one,

static??

static const int AL_INITIAL_CAPACITY = 16; serves no purpose in the .h file.

Order?

With so many functions, consider alphabetizing the order in both .c and .h.

Mixed indexing types

Code using int and size_t for the array indexing and sizing type. Suggest size_t throughout.

Pedantic growth

Insure capacity + capacity / 2 does not overflow.

More functions

Consider:

A right-size function to reduce the allocation to the needed size.

With al_find() and al_find_last(), perhaps a find_next(... index) to pickup after al_find()?

Consider an apply function, one that applies a passed in function to every element of the array. I'd such this is a better way to print too and delete al_print().

int al_apply(struct array_list *list, void *state, int (*f)(void *state, size_t index, int data));

Consider al_sort(int (*cmp)(const void *e1, const void *e2)).

2 of 4
6

This is a problem:

        list->array = realloc(list->array, sizeof(*(list->array)) * list->capacity);

If the allocation fails, then we have overwritten list->array with a null pointer, leaving no way to access the memory it was pointing to. That's a memory leak. Don't overwrite any values until we know our reallocation was successful:

    size_t new_capacity = capacity + capacity / 2;
    int *new_mem = realloc(list->array, sizeof *list->array * new_capacity);
    if (!new_mem) { return false; }  /* using <stdbool.h> */
    list-array = new_mem;
    list->capacity = new_capacity;