Good afternoon /prog/, its me again
i need to make a C funcion that returns a vector
so... how do i made C funcion return vector?
Name:
Anonymous2006-11-27 10:00
well, first, you have to make a struct called 'vector'. then have your C function return it.
alternatively, use C++, which actually /has/ vectors.
Name:
Anonymous2006-11-27 10:09
i cant use c++ cuz its my c class
thanks
Name:
Anonymous2006-11-27 10:16
use return;
Name:
Anonymous2006-11-27 10:19
oh, by the way, could you give me some code example please?
Name:
Anonymous2006-11-27 11:03
>>1
There are no vectors in C, but you can return a pointer to the first element of something. For example, an array of wchar_t:
wchar_t *CreateArray() {
//Create an array of, say 10 wchar_t
wchar_t *myarray = malloc(10 * sizeof(wchar_t));
//Return it
return myarray;
}
void ObtainArray() {
wchar_t *myarray; //We'll store the result here
//Call CreateArray to create the array
myarray = CreateArray();
}
Of course, this assumes you know the length of the array. If the function CreateArray doesn't know the length but ObtainArray does, you can make ObtainArray pass the length to CreateArray as an argument. If the length can be arbitrary and cannot be known beforehand, you'll have to mark it somehow. It's common for char and wchar_t strings to mark the end with a zero. For other stuff, you can define a struct like:
struct stuff {
int len; //Length
something *ptr; //Pointer to beginning
}
Name:
Anonymous2006-11-27 11:34
Avoiding the basics isn't going to help you learn.
In fact it'll make you a horrible programmer.
Name:
Anonymous2006-11-27 12:24
Do you mean a C++ style vector, or a mathematical type of vector?