C Programming
In this tutorial we will learn how to pass and use strings in functions in C programming language.
We know that a string is a sequence of characters enclosed in double quotes.
For example, "Hello World" is a string and it consists of a sequence of English letters in both uppercase and lowercase and the two words are separated by a white space. So, there are total 11 characters.
We know that a string in C programming language ends with a NULL \0
character. So, in order to save the above string we will need an array of size 12. The first 11 places will be used to store the words and the space and the 12th place will be used to hold the NULL character to mark the end.
We know that strings are saved in arrays so, to pass an one dimensional array to a function we will have the following declaration.
returnType functionName(char str[]);
Example:
void displayString(char str[]);
In the above example we have a function by the name displayString
and it takes an argument of type char
and the argument is an one dimensional array as we are using the []
square brackets.
To pass a one dimensional string to a function as an argument we just write the name of the string array variable.
In the following example we have a string array variable message
and it is passed to the displayString
function.
#include <stdio.h>
void displayString(char []);
int main(void) {
// variables
char
message[] = "Hello World";
// print the string message
displayString(message);
return 0;
}
void displayString(char str[]) {
printf("String: %s\n", str);
}
Output:
String: Hello World
Another way we can print the string is by using a loop like for
or while
and print characters till we hit the NULL character.
In the following example we have redefined the displayString
function logic.
#include <stdio.h>
void displayString(char []);
int main(void) {
// variables
char
message[] = "Hello World";
// print the string message
displayString(message);
return 0;
}
void displayString(char str[]) {
int i = 0;
printf("String: ");
while (str[i] != '\0') {
printf("%c", str[i]);
i++;
}
printf("\n");
}
Output:
String: Hello World
In order to accept two dimensional string array the function declaration will look like the following.
returnType functionName(char [][C], type rows);
Example:
void displayCities(char str[][50], int rows);
In the above example we have a function by the name displayCities
and it takes a two dimensional string array of type char
.
The str
is a two dimensional array as because we are using two [][]
sqaure brackets.
It is important to specify the second dimension of the array and in this example the second dimension i.e., total number of columns is 50.
The second parameter rows
tell us about the total number of rows in the give two dimensional string array str
.
The return type for this function is set to void
that means it will return no value.
To pass a two dimensional string to a function we just write the name of the string array variable as the function argument.
In the following example we have the name of 5 cities saved in an array cities
of type char
. We will be using the displayCities
function to print the names.
#include <stdio.h>
void displayCities(char [][50], int rows);
int main(void) {
// variables
char
cities[][50] = {
"Bangalore",
"Chennai",
"Kolkata",
"Mumbai",
"New Delhi"
};
int rows = 5;
// print the name of the cities
displayCities(cities, rows);
return 0;
}
void displayCities(char str[][50], int rows) {
// variables
int r, i;
printf("Cities:\n");
for (r = 0; r < rows; r++) {
i = 0;
while(str[r][i] != '\0') {
printf("%c", str[r][i]);
i++;
}
printf("\n");
}
}
Output:
Cities:
Bangalore
Chennai
Kolkata
Mumbai
New Delhi
ADVERTISEMENT