Can a function return a vector in C ? The reason why this question is asked, is because a function cannot return an array (that is similar to a vector) in C . The answer is simple. Yes, a function can return a vector in C and in different ways. This article explains the different ways in which a C function can return a vector.

In order to code a vector in C , the vector library has to be included in the program. The vector library has the vector class from which vector objects can be instantiated (created).

The program in which all the code samples of this article are, begins with:

#include

#include

#include

using namespace std;

A vector of strings is used.

Article Content

– Returning Vector by Normal Vector Name

– Returning a Vector Literal

– Returning a Vector Reference

– Returning a Vector Pointer

– Conclusion

Returning Vector by Normal Vector Name

Let the vector of interest be:

vector<string> store = {“bread”, “meat”, “rice”, “Tomato sauce”, “Cheese”};

The vector is a list of items in a small grocery store. The name, store of this vector, is to be sent as an argument to a function, whose parameter is a vector, but with the name, vtr. The function of interest can be:

vector<string> fn(vector<string> vtr) {

return vtr;

}

Notice the return type of the function definition. The name of the vector is store. This is the argument for the function call. The parameter for the function corresponding to the vector is:

Note that the argument for the function and the parameter name are different (they can still be the same). As soon as the function starts executing, the following statement is made:

vector<string> vtr = store;

This statement is equivalent to the following two statements:

vector<string> store = {“bread”, “meat”, “rice”, “tomato sauce”, “Cheese”};

vector<string> vtr = store;

And so, vtr is a copy of the vector, store. At this point, there are two vectors with the same content in memory for the program. An appropriate C main function for the code can be:

int main()

{

vector<string> v = fn(store);

for (int i=0; i<v.size(); i )

cout << v[i] << “, “;

cout << endl;

return 0;

}

Notice that the word store, is the argument of the function call. When the function is called, two copies of the same vector content occur in memory. The function (call) returns a vector, which is received by another vector, v. By the time the program finishes, there are three vectors of the same copy in memory. These three copies of the same content can be reduced to one copy by using a reference vector, or pointer vector. The output for the above program is:

bread, meat, rice, tomato sauce, Cheese,

Returning a Vector Literal

Today (in 2022), a vector literal is the same as an array literal. This literal is called an initializer_list, today in C . So, returning a vector literal by a function, is the same as returning an initializer_list. Let the initlializer_list be:

{“bread”, “meat”, “rice”, “tomato sauce”, “Cheese”}

Let the function definition to return the initializer_list be,

vector<string> fn() {

return {“bread”, “meat”, “rice”, “tomato sauce”, “Cheese”};

}

The initializer_list is composed on the spot in the return statement, and returned. The function definition has no parameter, but has the same return type as its counterpart in the previous section. Let the main C function be:

int main()

{

vector<string> v = fn();

for (int i=0; i<v.size(); i )

cout << v[i] << “, “;

cout << endl;

return 0;

}

The function call, this time, has no argument, but the return value is received by the same vector and type of the previous section.

By the time the program is completing, would there be two copies of the same vector in memory? No. There would be only one copy, which is v. The initializer_list is a kind of expression, called a rvalue. When this kind of expression is no longer needed in memory, can it be erased by C in order to have more memory space? It is not important whether it remains in memory after it has been used while the program continues to run. It would be erased if its space is needed. The program output is:

bread, meat, rice, tomato sauce, Cheese,

Returning a Vector Reference

The program here will do what the first program above has done, but only with one copy of the same vector. There will be three different names for the same vector, though. Let the vector of interest be:

vector<string> store = {“bread”, “meat”, “rice”, “tomato sauce”, “Cheese”};

The variable, store here, is an ordinary name. Let the function of interest be:

vector<string> & fn(vector<string> &vtr) {

return vtr;

}

Note the presence and position of & in the parameter. It means vtr is a referenced (synonym) vector, and not a copy of the argument to be sent. Note the presence and position of & in the return type. It means that the reference (synonym) of a vector will be returned by the function. Note that the inside statement, “return vtr;” does not have &. Let the C main function be:

int main()

{

vector<string> *v = &fn(store);

for (int i=0; i<v->size(); i )

cout << (*v)[i] << “, “;

cout << endl;

return 0;

}

The signature of the function definition, and the function call statement, are:

vector<string> & fn(vector<string> &vtr)

and

vector<string> *v = &fn(store);

respectively. Note again, the presence and position of &, in the return type of the function definition. Note the presence and position of & in the function call statement. The argument of the function call is the ordinary name of the vector, store. The function returns a reference, and it is received by a pointer, v.

And so, there are three different variables in the program, all referring to the same vector memory location (the function returned &vtr, which is a synonym for store). The output is:

bread, meat, rice, tomato sauce, Cheese,

Returning a Vector Pointer

The program here will do what the first program above has done, but only with one copy of the same vector. There will be three different names for the same vector. Let the vector of interest be:

vector<string> store = {“bread”, “meat”, “rice”, “tomato sauce”, “Cheese”};

The variable, store here, is an ordinary name. Let the function of interest be:

vector<string> * fn(vector<string> *vtr) {

return vtr;

}

Note the presence and position of * in the parameter. It means vtr is a pointer vector, and not a copy of any vector argument to be sent. Note the presence and position of * in the return type. Again, note that the inside statement, “return vtr;” does not have & or *. Let the C main function be:

int main()

{

vector<string> *v = fn(&store);

for (int i=0; i<v->size(); i )

cout << (*v)[i] << “, “;

cout << endl;

return 0;

}

The signature of the function definition, and the function call statement, are:

vector<string> * fn(vector<string> *vtr)

and

vector<string> *v = fn(&store);

respectively. Note the presence and position of * in the return type of the function definition. Note the presence and position of & in the function call statement; it is in front of the argument, store, and not in front of fn(), which does not have & or *. The function returns a reference, and it is received by a pointer, v.

And so, there are three different variables in the program, all referring to the same vector memory location. The output is:

bread, meat, rice, tomato sauce, Cheese,

Conclusion

A function can return a vector by its normal name. A function can return a vector literal (initializer_list), to be received by a normal vector (name). A vector can return a vector reference, to be received by a vector pointer. A vector can return a vector pointer, still to be received by another vector pointer.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Chrysanthus-150×150.jpeg6265c66e6d34a.jpg" height="112" src="data:image/svg xml,” width=”112″>

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.