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 - // C# Array to List Console.WriteLine("C# Array to List Example!"); // Array of string items string[] names = { "Mahesh Chand", "Doug Wagner", "Neel Beniwal" }; // Covert array to List #1 Console.WriteLine("----AddRange method----"); List<string> list = new(); list.AddRange(names); foreach(string name in names) Console.WriteLine(name); // Convert array to List #2 Console.WriteLine("----ToList method----"); List<string> namesList = names.ToList(); foreach (string name in namesList) Console.WriteLine(name); Console.ReadKey();
🌐
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 - Linked List: Convert a Singly Linked list into an array: ------------------------------------------------------------- Input the number of nodes: 4 Input data for node 1 : 10 Input data for node 2 : 20 Input data for node 3 : 30 Input data for node 4 : 40 Return data entered in the list as an array: 10 20 30 40 ... Write a C program to convert a singly linked list into a dynamically allocated array.
🌐
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.
🌐
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.
Find elsewhere
🌐
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 …
🌐
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.
🌐
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 - namespace ConsoleApplication8 { class Program { static void Main(string[] args) { //creation of list<> List<int> number = new List<int>(); //add the elements in the list number.Add(10); number.Add(20); number.Add(15); number.Add(25); number.Add(30); number.Add(35); number.Add(40); number.Add(45); //creation of an array int[] a = number.ToArray(); //display the array element Console.WriteLine("the array elements are:"); foreach (int i in a) { Console.WriteLine(i); } } } } ... In this example, we see how to convert an array of any number of elements to a List that has the same type of elements.
🌐
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
A List can be converted to an array. The opposite conversion is also possible. In each conversion, the element types remain the same—strings remain strings.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.collections.generic.list-1.toarray
List<T>.ToArray Method (System.Collections.Generic) | Microsoft Learn
You can try changing directories. ... Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here. Copies the elements of the List<T> to a new array.
🌐
LabEx
labex.io › tutorials › java-converting-between-an-array-and-list-117399
Array to List Conversion | Java Programming | LabEx
The asList() method of the Arrays class can be used to convert an array to a list. It takes the array as a parameter and returns a list of its elements. We will perform the following steps to convert an array to a list using the asList() method: ...
🌐
Techie Delight
techiedelight.com › home › c++ › convert a list to an array in c++
Convert a list to an array in C++ | Techie Delight
July 7, 2026 - A simple solution is to use a range-based for-loop to traverse the list and, one by one, add each element at the next available index in the array. For example, if we want to convert the list {1, 2, 3, 4, 5} to an array of the same type and ...
🌐
Reddit
reddit.com › r/unity3d › optimize converting array to list c#
r/Unity3D on Reddit: Optimize Converting Array to List c#
August 5, 2022 -

I'm converting an Array of Vector3s and I'm trying to make it run faster. Its easy to convert an array to a list in one line, but now I need to convert the list<Vector3> to a List<List<float>> without using a for loop

Current Code:

            //get array of vertex positions(Vector3)
            Vector3[] verts = savemeshes[currentMesh].mesh.vertices;

            //convert to list of vector3s
            List<Vector3> vertsList = new(verts);

            //convert to list of list of floats
            List<List<float>> vertsFloatList = new();
            for (int i = 0; i < vertsList.Count; i++)
            {
                vertsFloatList.Add(new()
                {
                    vertsList[i].x,
                    vertsList[i].y,
                    vertsList[i].z
                });
            }