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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/01.png" data-lazy- height="35" src="data:image/svg xml,” width=”780″>

OR

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/02.png" data-lazy- height="25" src="data:image/svg xml,” width=”345″>

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’,…’′).

For terminating character sequence or string, a NULL character is used, which is . It is a good practice to end an array of characters with a while initializing. However, when the compiler comes across a double quotation marked a string of characters, it adds 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:

  1. Normally, passing a string array.
  2. Pass the string using pointers.

Example 1

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-01.png" data-lazy- height="155" src="data:image/svg xml,” width=”780″>

Now go to your windows cmd and type the following listed instruction:

$ gcc -o [your filename] [your filename].cpp

GCC is a C compiler; by typing the following command, we tell the compiler to compile our .cpp file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-02.png" data-lazy- height="60" src="data:image/svg xml,” width=”493″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-03.png" data-lazy- height="30" src="data:image/svg xml,” width=”315″>

[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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-04.png" data-lazy- height="115" src="data:image/svg xml,” width=”405″>

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).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-05.png" data-lazy- height="125" src="data:image/svg xml,” width=”505″>

Now we, need to create a function call for the getstring function and pass the string variable in it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-07.png" data-lazy- height="248" src="data:image/svg xml,” width=”635″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-08.png" data-lazy- height="95" src="data:image/svg xml,” width=”780″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-09.png" data-lazy- height="145" src="data:image/svg xml,” width=”630″>

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.

Example 2

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-10.png" data-lazy- height="75" src="data:image/svg xml,” width=”345″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-11.png" data-lazy- height="393" src="data:image/svg xml,” width=”780″>

Let’s test our code to see if it was successful or not.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/pass-string-function-C-language-12.png" data-lazy- height="110" src="data:image/svg xml,” width=”520″>

We can see clearly from the screenshot above that our program is passing the string to the function without a hitch using a pointer.

Conclusion

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.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

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