Hello,
I would like to know if there is any way to return an char array. I tried something like this "char[] fun()" but I am getting error.
I don't want a pointer solution. Thanks!
Hello,
I would like to know if there is any way to return an char array. I tried something like this "char[] fun()" but I am getting error.
I don't want a pointer solution. Thanks!
arrays aren't 1st class objects in C, you have to deal with them via pointers, if the array is created in your function you will also have to ensure its on the heap and the caller cleans up the memory
Arrays cannot be passed or returned by value in C.
You will need to either accept a pointer and a size for a buffer to store your results, or you will have to return a different type, such as a pointer. The former is often preferred, but doesn't always fit.
You can return an array by wrapping it in a struct:
struct S {
char a[100];
};
struct S f() {
struct S s;
strcpy( s.a, "foobar" );
return s;
}