Home > Software engineering >  c Is it possible to have a process (outside of main) that returns an array whose size is determine
c Is it possible to have a process (outside of main) that returns an array whose size is determine

Time:12-24

Bellow is a simple program that works fine. It contains a function that is able to return a string of arbitrary size. The size of which is determined by the function input.

#include <iostream>                                                                                                                                                                                                                           
                                                                                                                                                                                                                                              
using namespace std;                                                                                                                                                                                                                          
                                                                                                                                                                                                                                              
string strFunc(int a){                                                                                                                                                                                                                        
                                                                                                                                                                                                                                              
  string toBeReturned;                                                                                                                                                                                                                        
                                                                                                                                                                                                                                              
  for(int i=0; i < a; i  ){                                                                                                                                                                                                                   
    toBeReturned  = '!';                                                                                                                                                                                                                      
  }                                                                                                                                                                                                                                           
  return toBeReturned;                                                                                                                                                                                                                        
}                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                              
int main(){                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                              
  int x = 5;                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                              
  cout << strFunc(x) << endl;                                                                                                                                                                                                                 
                                                                                                                                                                                                                                              
  return 0;                                                                                                                                                                                                                                   
}             

                                                                                                                                                                                                                            

If instead I wanted a function (or a single process to call in main) to return a 1-D array (int toBeReturned[size to be determined]) I had to use a function that returns a pointer and then include that function in a macro that constructs the array.

Is there a simpler way of doing this in c ?

If not can someone please explain why this only works for type string? I thought that a string is simply a 1-D array of type 'char'.

Thank you,

Daniel

CodePudding user response:

You can use a vector of char, and pass it into strFunc by reference.

CodePudding user response:

A function can return any POD or class type by value.

A C -style std::array is a fixed-sized array wrapped in a class type, and thus can be returned by value. However, a C-style fixed-sized array cannot be returned by value (but it can be stored as a member of a class type, which can then be returned by value, like std::array does).

A C-style array can't be sized dynamically (without using a non-standard compiler extension), which is why you would have to new[] it, return it by pointer, and then delete[] it when you are done using it.

Since you want your function to return a dynamic-sized array, you should use std::vector instead of a new[]'ed pointer, eg:

#include <iostream>                                                                                                                                                                                                                           
#include <vector>                                                                                                                                                                                                                                        
using namespace std;                                                                                                                                                                                                                          

vector<int> strFunc(int a){                                                                                                                                                                                                                        

  vector<int> toBeReturned(a); 

  for(size_t i = 0; i < a;   i){
    toBeReturned[i] = ...;                                                                                                                                                                                                                      
  }

  return toBeReturned;
}                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                              
int main(){                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                              
  int x = 5;
                                                                                                                                                                                                                                              
  vector<int> returned = strFunc(x);
  for(size_t i = 0; i < x;   i){
    cout << returned[i] << ' ' << endl;
  }
                                                                                                                                                                                                                                              
  return 0;                                                                                                                                                                                                                                   
} 
  • Related