Multiple pointers

Carey Evans careye at spamcop.net
Sun Jan 6 11:02:48 CET 2002


wilfartg at hotmail.com (Geoffrey Wilfart) writes:

> I would like to export to Python a C function that takes pointers of
> pointers as arguments, and fills them.

[...]

>   void my_function(char ***arg), associated to:
> 
> a = [[]]
> wrapper(a)

Actually, I'd create a new list from within wrapper(), and return it.
With functions like PyList_SET_ITEM, this is actually considerably
clearer than modifying an existing list.

I'm assuming that the size of the data is known beforehand, since
you're not passing any parameters to my_function() to say how big each
list is.  In that case, this code fragment should get you started.


	my_function(data);

	list = PyList_New(ROWS);
	if (list == NULL)
		return NULL;

	for (i = 0; i < ROWS; i++) {
		PyObject *sublist = PyList_New(COLS);
		if (sublist == NULL) {
			Py_DECREF(list);
			return NULL;
		}
		PyList_SET_ITEM(list, i, sublist);

		for (j = 0; j < COLS; j++) {
			PyObject *string = PyString_FromString(data[i][j]);
			if (string == NULL) {
				Py_DECREF(list);
				return NULL;
			}
			PyList_SET_ITEM(sublist, j, string);
		}
	}

	return list;


-- 
	 Carey Evans  http://home.clear.net.nz/pages/c.evans/

                             Cavem canus.



More information about the Python-list mailing list