C++

C++ Return Char Array From Function

Arrays are a particular form of variable that allows users to manage different sets of values (integer, character, string, etc.) in a single memory space. The indexes make it simple to identify the characters or elements.  C++ data type named Char is used to store characters. A character is abbreviated as Char. The data is saved as an absolute value because this is an integral data set. A char occupies one byte of memory.

Like an array of floating numbers or integers, every array index includes one element or character. We will discuss how to return a char array from a function in C++ in this article:

Use ‘for’ Loop to Return Character Array

In this code, we will be using a for loop in C++ to return an array of characters. We will construct the Character Array and then specify the size of that Array. Then we define an ‘integer’ data type variable. We can use the ‘for’ loop by allocating 0 to the ‘i’ variable, which would have a smaller value than the size of the array, and enhance the value of the ‘i’ by 1 at each loop. Before presenting the character array, we must retain the data in the variable on each iterative process.

#include <iostream>

#include <conio.h>

using namespace std;
int main()
{
    int i;
    char char_arr[6];
char_arr[0]='L';
char_arr[1]='M';
char_arr[2]='N';
char_arr[3]='O';
char_arr[4]='P';
char_arr[5]='Q';
cout<<"\nshow all the characters:\n";
for(i=0; i<6; i++)
    {
cout<<"char_arr["<<i<<"]:"<<char_arr[i];
cout<<("\n");
    }
getch();
    return 0;
}

Here we have to introduce two important header files <iostream> and <conio.h>. Along with this, we have been using the standard namespace. The main() function is invoked. We have initialized a variable ‘i’. We declare the character array and set the size of the array. There would be the character ‘ L’ in the ‘0’ index of the array. At the ‘1’ index, there would be the character ‘M’. At the ‘2’ index, there would be the character ‘N’. At the ‘3’ index, there would be character ‘O’. At the ‘4’ index, there would be the character ‘P’. At the ‘5’ index, there would be the character ‘Q’.

The ‘cout’ command is applied to print the line ‘show all the characters’ before displaying the character array elements. We display the defined character array members by using a for a loop. Within the body of the ‘for’ loop first, we start the loop variable ‘i’. We apply the condition on this variable ‘i<6’, and in the last part, we increment the value of this variable. We have utilized the ‘cout’ command to show the elements of the defined array. ‘\n’ is used to print the elements on separate lines.

In the end, we have entered getch() and ‘return 0’. In the code, we utilize the getch() method to keep the output display available till the user enters any key on the keyboard to close the output screen.

Use ‘while’ Loop to Return Character Array

In this instance, we will demonstrate how to utilize the while loop in C++ to return a character array. Before presenting the character array, we will be using a while loop to validate the ‘int’ variable smaller than the size of the array on each iteration and save the value in a character array.

#include <iostream>

#include <conio.h>

using namespace std;
int main()
{
    int i;
    char char_arr[]={'A','L','E','E','N','A','A'};
cout<<"\nshow all the characters:\n";
i=0;
      while(i<7)
    {
cout<<"char_arr["<<i<<"]:"<<char_arr[i];
cout<<("\n");
i++;
    }
getch();
    return 0;
}

Two libraries, <iostream> and <conio.h>, must be included at the program’s start. We’ve been employing the standard namespace. The function main() is being called. We’ve set up a variable called ‘i’ within the body of this method. Now the character array is declared. The components of the character array have been specified. The character ‘A’ will be at the array’s ‘0’ index. The character ‘L’ will be at index ‘1.’ Character ‘E’ will be found at the ‘2’ index. Character ‘E’ will be found at index ‘3’. Character ‘N’ will be found at index ‘4’. Character ‘A’ will be found at index ‘5.’ Character ‘A’ will be found at index ‘6.’

Before presenting the components of the character array, the ‘cout’ statement is used to display the text ‘show all the characters.’ The ‘while’ loop is being applied to illustrate the elements of the defined character array. We initially define the loop variable ‘i’ just outside the body of the ‘while’ loop. We have used the ‘cout’ statement to display the items of the declared array with their positions after applying the condition on the variable ‘i<7’. ‘\n’ would be applied to display every element on its line. We’ve added getch() and ‘return 0’ commands at the termination of the code.

To Return a Character Array, Utilize a ‘do-while’ Loop

In this case, we will use a do-while loop in C++ to return an array of characters.

#include <iostream>

#include <conio.h>

using namespace std;
int main()
{
    int i;
    char char_arr[]={'P','Y','T','H','O','N'};
cout<<"\nshow all the characters:\n";
i=0;  
    do
    {
cout<<"char_arr["<<i<<"]:"<<char_arr[i];
cout<<("\n");
i++;
    }  
    while(i<6);
getch();
    return 0;
}

First of all we integrate the header files <iostream> and <conio.h>. After using ‘namespace’, we invoked the main() function. The variable ‘i’ is initialized for storing the character array elements. We specify the members of the character array by declaring this array. We have to enter the ‘cout’ command to print the phrase just before the character array elements after initializing the variable ‘i’. We have employed a do-while loop. In the segment of ‘do,’ we have used the ‘cout’ statement to display all the elements of the required array. Then we apply condition ‘i<6’ in the body of the while portion. To terminate the code, we employ ‘return 0’.

Conclusion

The character arrays in the C++ language have been reviewed in this article. Character arrays are sets of characters held in the form of arrays. The preceding discussion includes three basic techniques for returning the character array from the function.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content