Returns an array in c

I would like to know if there is a way to return a char array. I tried something like this "char [] fun ()", but I get an error.

I do not want to use a pointer. Thanks!

+4
source share
5 answers

You can return an array by wrapping it in a struct:

struct S { char a[100]; }; struct S f() { struct S s; strcpy( sa, "foobar" ); return s; } 
+11
source

Arrays cannot be passed or returned with a value in C.

You will need to either accept a pointer or a size for a buffer to hold the results, or you will have to return another type, such as a pointer. The first is often preferable, but not always suitable.

+5
source

C functions cannot return array types. The types of returned functions can be any, except for "array T" or "function returning T". Also note that you cannot assign array types; that is, code like the following will not work:

 int a[10]; a = foo(); 

Arrays in C are handled differently than other types; in most contexts, the type of an array expression is implicitly converted ("decomposed") from an "N-element array T" to a "pointer to T", and its value is set to point to the first element in the array. The exceptions to this rule are when the array expression is an operand of either sizeof operators or addresses ( & ), or when the expression is a string literal used to initialize another array in the declaration.

Given an announcement

 T a[N]; 

for any type T, then the following is true:

  Expression Type Decays to Notes
 ---------- ---- --------- -----
          a T [N] T * Value is address of first element
         & a T (*) [N] n / a Value is address of array (which
                                           is the same as the address of the
                                           first element, but the types are
                                           different)
   sizeof a size_t n / a Number of bytes (chars) in array = 
                                           N * sizeof (T)
 sizeof a [i] size_t n / a Number of bytes in single element = 
                                           sizeof (T)
        a [i] T n / a Value of i'th element
       & a [i] T * n / a Address of i'th element

Due to the implicit conversion rule, when you pass an array argument to a function, what the function receives is the value of the pointer, not the value of the array:

 int a[10]; ... foo(a); ... void foo(int *a) { // do something with a } 

Please note that something like

 int *foo(void) { int arr[N]; ... return arr; } 

does not work; one of the functions comes out, the arr array technically no longer exists, and its contents can be overwritten before you can use it.

If you do not dynamically allocate buffers, it is best to pass the arrays that you want to change as arguments to the function along with their size (since the function receives only the pointer value, it cannot determine how large the array is):

 int a[10]; init(a, sizeof a / sizeof a[0]); // divide the total number of bytes in ... // in the array by the number of bytes void init(int *a, size_t len) // a single element to get the number { // of elements size_t i; for (i = 0; i < len; i++) a[i] = i; } 
+4
source

arrays are not objects of the first class in C, you have to deal with them using pointers, if the array is created in your function, you also need to ensure its presence on the heap, and the calling user clears the memory

+3
source

A Very simple code and a very simple explanation of HOW to return an array returns from a user-defined function to the main function. helps !! Below I gave the full code so that someone can understand how it works? :) :)

 #include<iostream> using namespace std; char * function_Random() { int i; char arr[2]; char j=65;//an ascII value 65=A and 66=B cout<<"We are Inside FunctionRandom"<<endl; for(i=0;i<2;i++) { arr[i]=j++;// first arr[0]=65=A and then 66=B cout<<"\t"<<arr[i]; } cout<<endl<<endl; return arr; } int main() { char *arrptr; arrptr=function_Random(); cout<<"We are Inside Main"<<endl; for(int j=0;j<2;j++) { cout<<"\t"<<arrptr[j]; } return 0; } 
-1
source

Source: https://habr.com/ru/post/1303647/


All Articles