It seems that data structures (because C can obviously return a locally declared variable) declared locally within a function cannot be returned, in this case an array.
You already have a good Python answer; I wanted to look at the C side a little more closely.
Yes, a C function returns a value. That value may be primitive C type, or a struct
or union
type. Or, it may be a pointer type.
The C language syntax makes arrays and pointers seem very similar, which makes arrays special. Because the name of the array is the same as the address of the first element, it can't be something else. In particular, an array name does not refer to the whole array (except in the case of the sizeof
operator). Because any other use of an array name refers to the address of the first element, attempting to return an array results in returning only that address.
Because it's a C function, that address is returned by value: namely, a value of a pointer type. So, when we say,
char *s = strdup("hello");
s
is a pointer type whose value is not "hello", but the value of address of the first element of the array that strdup
allocates.
Python doesn't suffer from the same problem
When Y is a property of X, Y is a problem only if that property is, in the eyes of the beholder, undesirable. You can be sure the way C treats arrays is not accidental, and is often convenient.