You are returning a pointer to a stack memory location. That memory region is no longer valid once the function returns.

Also, instead of actually returning a pointer to the array, you are returning the first element in the array. The following code will return 1, not a pointer the array.

int array[] {1, 2, 3, 4};
return *array

You probably only need to make minimal changes your code to get it to work.

void** array = (void **) malloc(sizeof(void *) * array_size); 
...
return array;

Just make sure that you release the memory that memory used for array when you are finished with it.

void **array = list_to_array(list);
// Use array
...
// Finished with array
free(array);
Answer from torak on Stack Overflow
Top answer
1 of 2
1

The last node of the list ends up with a NULL next and a garbled value. I don't think this is what you wanted. Try this loop:

struct List* head = NULL;
struct List** tail = &head;

for(int i = 0; i < length; i++) {
    *tail = malloc(sizeof(struct List));
    if (*tail == NULL) {
        listDestroy(head, freeElement);
        return NULL;
    }
    tail[0]->value = copyElement(array[i]);
    tail = &(tail[0]->next);
}
*tail = NULL;

return head;

This uses a pointer to pointer so that we can update head or next depending on where we are in the loop without an extra if condition. Note that the allocaction of head is inside the loop now, so we always allocate exactly as many nodes as we need.

2 of 2
1

void* array[] is completely valid in this scenario, although I don't see the point of copyElement.
Perhaps just remove it and use array[i] instead?

I'd also like to point out a few mistakes that you may want to fix in your code:

Don't cast the result of malloc and use sizeof(*var) instead of sizeof(type). They're unneeded and may cause issues later on if you change the type of head. So, change this (and any later occurrences):

    struct List* head = (struct List*) malloc(sizeof(struct List));

to this:

    struct List* head = malloc(sizeof(*head));

Consider returning struct List * from arr2list. Change this:

void* arr2list(void* array[], int length, void* copyElement(void*), void freeElement(void*)) {

to this:

struct List* arr2list(void* array[], int length, void* copyElement(void*), void freeElement(void*)) {

Also, use either struct List * or List *. Inconsistencies may make it hard to understand your code.

It may be clearer if you pass function pointers to your functions instead of just functions. So, instead of doing this:

returntype func(params),

do this:

returntype (*func)(params),

You may also want to change malloc calls to calloc calls. calloc zeroes out memory, which can help in debugging. Change:

malloc(...)

to:

calloc(1, ...)

Here's an example combining code fixes from Joshua's post and mine:

list.c:

#include <stdlib.h>

typedef struct List {
    struct List* next;
    void *value;
} List;

void listDestroy(struct List* list, void (*freeElement)(void*)) {
    while(list != NULL) {
        freeElement(list->value);
        struct List* temp_node = list;
        list = list->next;
        free(temp_node);
    }
}

struct List* arr2list(void* array[], int length, void (*freeElement)(void*)) {
    struct List* head = NULL;
    struct List** tail = &head;

    if (length == 0 || !freeElement) {
        return NULL;
    }

    for(int i = 0; i < length; i++) {
        *tail = calloc(1, sizeof(struct List));
        if (*tail == NULL) {
            listDestroy(head, freeElement);
            return NULL;
        }
        tail[0]->value = array[i];
        tail = &(tail[0]->next);
    }
    *tail = NULL;

    return head;
}

test.c:

#include <stdio.h>
#include <stdlib.h>

#include "list.c"

void free_element(void *el)
{
        free(el);
}

int main(void)
{
        List *list;
        int ctr;
        void *array[4];
        for(ctr = 0; ctr < 4; ctr++)
        {
                /* Don't usually pass sizeof(type) to malloc(),
                 * but these are extenuating circumstances. */
                array[ctr] = malloc(sizeof(int));
                if(!array[ctr]) return 1;
                *(int *)array[ctr] = ctr * 4;
        }
        list = arr2list(array, sizeof(array)/sizeof(array[0]), free_element);
        while(list)
        {
                printf("%d\n", *(int *)list->value);
                list = list->next;
        }
        listDestroy(list, free_element);
        return 0;
}

Output:

0
4
8
12

One last note: you may consider creating a header file that defines these structures and functions (and remove the structure from list.c). Here's an example:

#ifndef LIST_H
#define LIST_H 1

typedef struct List { // Remove this from list.c
    struct List* next;
    void *value;
} List;

void listDestroy(struct List*, void (*)(void*));

struct List* arr2list(void* [], int, void (*)(void*));

#endif
Discussions

c# - Conversion of System.Array to List - Stack Overflow
Note for completeness: the 2nd method is only available in C# 3.0+. 2009-10-21T20:17:13.21Z+00:00 ... Since the int array already implements IEnumerable, the OfType() is not required. ints.ToList(); suffices. 2012-02-29T17:06:49.793Z+00:00 ... None of these examples actually answer the actual question. But I guess he accepted the answer so he was happy. Still, not one of these actually convert an Array to a List... More on stackoverflow.com
🌐 stackoverflow.com
Need help with converting an array to a list in C#` - Questions & Answers - Unity Discussions
Okay, I’m bringing in xml data, so I assume that has to be done with an array, or at least is done that way in this example: http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer . Now since that part works, I don’t want to mess around with it unless I have to, ... More on discussions.unity.com
🌐 discussions.unity.com
0
March 9, 2014
C# array to list
I do not want to make a loop to go from array to list. ToList() does not work new List (myArray) does not work List list = myArray.ToList (); does not work ConvertAll is not clear to me Do you know what the right code is? question8.gh (4.9 KB) More on discourse.mcneel.com
🌐 discourse.mcneel.com
2
0
September 14, 2020
How to make a list
What is the data type of each input? How many such inputs are there? Is the number of inputs known before runtime, or does it depend on the user? Any solution depends on the answers to these questions, but two general approaches are arrays and linked lists; for the latter, you will need to define structures and functions on your own. More on reddit.com
🌐 r/cprogramming
4
3
October 9, 2022
🌐
C# Corner
c-sharpcorner.com › home › technologies › how do i › how to convert an array to a list in c#?
How to Convert an Array to a List in C#?
October 10, 2023 - Converting an array to a list in C# involves transforming the data structure from an array, which has a fixed size, into a list, which can dynamically adjust its size. This process is essential when flexibility in modifying the collection is needed. In C#, this can be achieved using the `ToList()` ...
🌐
YouTube
youtube.com › watch
C# How to convert Arrays to Lists - Solving Problems - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published: February 28, 2023
🌐
w3resource
w3resource.com › c-programming-exercises › linked_list › c-linked_list-exercise-32.php
C Exercises: Convert a Singly Linked list into a array
October 16, 2025 - Write a C program to convert a linked list into an array and then sort the array elements.
🌐
Unity
discussions.unity.com › archived forums › questions & answers
Need help with converting an array to a list in C#` - Questions & Answers - Unity Discussions
March 9, 2014 - Okay, I’m bringing in xml data, so I assume that has to be done with an array, or at least is done that way in this example: http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer . 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.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › csharp › array to list in csharp
How to Convert Array to List in C# | Delft Stack
March 11, 2025 - This method allows you to add multiple elements to a list in one go, making it a flexible option for converting arrays. Unlike the ToList() method, AddRange() requires you to first create an instance of the list and then add the array elements to it.
🌐
GeeksforGeeks
geeksforgeeks.org › c# › how-to-convert-integer-array-to-list-in-c-sharp
How to Convert Integer Array to List in C#? - GeeksforGeeks
July 15, 2025 - Method 2: List<T>(IEnumerable<T>) Constructor : Using this constructor a new instance of the List<T> class can be initialize that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied. Thus this can also be used to convert a given an integer array to the list .
🌐
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 (myArray) does not work List list = myArray.ToList (); does not work ConvertAll is not clear to me Do you …
🌐
C# Corner
c-sharpcorner.com › UploadFile › c25b6d › conversion-from-list-to-array-and-vice-versa
How to convert C# List to an array?
September 8, 2023 - Using a list element as an array by converting the list into an array and array into a list; also, how to find the maximum and minimum element in the list.
🌐
Stackademic
blog.stackademic.com › converting-arrays-to-lists-and-adding-elements-in-c-7384c877be3b
Converting Arrays to Lists and Adding Elements in C# | by codezone | Stackademic
November 1, 2023 - List<int> listFromArray = new List<int>(originalArray); Now that you have the array as a list, you can easily add elements to it.
🌐
Cplusplus
cplusplus.com › forum › general › 25731
how can I copy my array into a list? - C++ Forum
July 4, 2010 - In this case you should do it like this -> list<int> tempList (tempSet, tempSet + mySides); You'll say "but I saw the list reference and I swear it did it like I do it here!..." (*) True, but notice that the example there uses an array. You use a pointer here.
🌐
Educative
educative.io › answers › how-to-convert-a-c-sharp-list-to-an-array
How to convert a C# list to an Array
This method uses the Array.Copy() method internally to copy the list elements to a new array. Since it copies all the elements, the time complexity of this method is O(n), where n is the number of elements. In the code example below, we create a list of integers and convert it to an array of ...
🌐
Techie Delight
techiedelight.com › home › c++ › convert an array to a list in c++
Convert an array to a list in C++ | Techie Delight
July 7, 2026 - For an existing list, we can use the overloaded std::insert function that takes three parameters. The first parameter is an iterator to the destination list. The second and third parameters are the iterators specifying a range of the array elements.
🌐
Dot Net Perls
dotnetperls.com › convert-list-array
C# - Convert List to Array - Dot Net Perls
We can convert an array of any number of elements to a List that has the same type of elements. There are 3 parts to this example. Part 1 Here we initialize a new string array containing 5 strings.