Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
This page presents the sometimes confusing way in which C handles the passing of array structures.
2. Call by reference
In C, a string variable is actually an array of characters.
In C, an array is always passed by reference. This means that the parameter passed can be changed.
3. Technical point on array input
Technical point: Assume the following string declarations.
In C, a string is actually an array of characters.
4. Array input
Then, in C, the following produce the same result.
Note: The address of the array s1 and the address of the array element s1[0] are the same address.
Note: A scanf for a string using "%s[^\n]" will cause an error if there is not "\n" (newline) at the end of the file. Here, just text until a space or end of file is input.
Previously, the address-of operator, ampersand "&", was used for all variables to scanf to have a consistent notation.
In passing parameters in general, we will usually stop using the ampersand "&" for arrays in general and strings as arrays of characters in particular.
5. Output
The output is in the usual manner.
6. Program
Here is a program to illustrate this.
Here is the C code.
7. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.