Before we dive into the main course scenario of passing the string to a function, let us give you a basic introduction of what stings are. Strings are a sequence or array of characters known as a char data type in C language. The reason why we stated it is an array of characters is that when user types:
As a whole, both lines mean it is just a string of characters that uses an array of char variables for storing. A string is enclosed in a double quotation mark (i.e., “ijklm”). Unlike a character enclosed in a single quotation mark per character (i.e. ‘i’, ‘j’,…’\0′).
For terminating character sequence or string, a NULL character is used, which is \0. It is a good practice to end an array of characters with a \0 while initializing. However, when the compiler comes across a double quotation marked a string of characters, it adds \0 at its end by default.
That’s enough overview about strings to let us get to the main task of passing the string to a function. There might be many ways of doing this task. In this tutorial, we will be deliberating two methods for performing this task:
- Normally, passing a string array.
- Pass the string using pointers.
In our first example, we will pass the string to a function that is done for any other ordinary array (i.e., float, integer, or double array). Open a notepad and give it the name of your choice. We are naming it as “myprogram.cpp”
.cpp is an extension used at the end of C programing files so that the compiler during execution knows this file contains C language code. Other relevant information can be added as well.
Now go to your windows cmd and type the following listed instruction:
GCC is a C compiler; by typing the following command, we tell the compiler to compile our .cpp file.
After file compilation, an exe file for your newly created notepad file must have been created. For its execution, we will type the listed query in the cmd:
[Your Filename] will be replaced by the name of your created file. Now we shall fill our notepad file with c language code. Starting from function declaration, we need to declare our string function.
The return type is kept void because it will display a success message on the successful passing of sting into the function. The parameter for my getstring function is the character type array variable because that is how the ordinary array passing functions are declared.
Our main function character array will be declared and initialized with an index size of 50 (assuming this size will be enough for this example). After array declaration, we will store user input in our newly declared array variable using the gets function (gets function is used for fetching string input directly without using for or any other kind of loop).
Now we, need to create a function call for the getstring function and pass the string variable in it.
Let us commence coding for our getstring function. We have passed the string argument from the main function. Create a function definition and display the string parameter for assurance.
That’s all. Let’s test our code to see if it was effective or not.
As you can see, the success message is successfully displayed, with the string passed into the get string function. It means our program has been executed effectively.
In this example, we will try to pass a string into the function using pointers. The drill for coding is the same as before starting, from changing the function declaration. Instead of passing an array of characters, we will pass a string pointer. That way, the string’s address will be passed down to the function, using that address string will be fetched out and displayed on the console. For declaring the pointer, we need to type * with any variable name.
After passing the pointer, all we need to do is tweak up the function definition. For passing the string pointer from the main function to the getstring function. Similar to the way done in the code mentioned below.
Let’s test our code to see if it was successful or not.
We can see clearly from the screenshot above that our program is passing the string to the function without a hitch using a pointer.
This article covered what strings are, the difference between character/string, and a few examples for passing a string to a function. The examples can be utilized according to your working requirements. I hope this tutorial was easy to understand and clear all your queries related to passing the string to a function in the C language.