How to quickly set a list in C?

Hello!

Is there a quick way to create a list in C? Like: VuoList_VuoReal something = [listItem1, listItem2,…]?

1 Like

Well, not really. You could do:

// Start with an array.
const size_t COUNT = 3;
VuoReal src[COUNT] = { 4.0, 5.0, 6.0 };

// Create a list containing 3 items each initialized to 0.
VuoList_VuoReal list = VuoListCreateWithCount_VuoReal(COUNT, 0.0);

// Get a pointer to the array containing the list items.
VuoReal *dst = VuoListGetData_VuoReal(list);

// Copy the contents of your array into the list's array.
memcpy(dst, src, COUNT * sizeof(VuoReal));

Or this would be more concise though less flexible (e.g. wouldn’t handle variables as list items):

// Start with the items in a JSON-formatted string.
const char *src = "[4.0, 5.0, 6.0]";

// Create a JSON object from the string.
json_object *js = json_tokener_parse(src);

// Create a list from the JSON object.
VuoList_VuoReal list = VuoList_VuoReal_makeFromJson(js);

// Release the JSON object so it gets deallocated.
json_object_put(js);
1 Like

Or, as of Vuo 2.4.1:

// Start with an array.
const size_t COUNT = 3;
VuoReal src[COUNT] = { 4.0, 5.0, 6.0 };

// Create a list from the array.
VuoList_VuoReal list = VuoListCreateWithValueArray_VuoReal(src, COUNT);
2 Likes