Home »
Code Snippets »
C/C++ Code Snippets
Passing two dimensional character array (string array) into a function
By: IncludeHelp On 06 DEC 2016
In this program we will learn how to pass two dimensional character arrays (string array) into a function?
Here, we are declaring a 3x10 character array (number of rows left black compiler will occupy memory according to string initialization, number of columns are for each row is 10 that means a string can contain maximum of 10 characters).
Function description
void dispStrings(char x[][10],int n)
This function will print the string arrays (two dimensional character arrays) passed through x and; while passing two dimensional arrays there is no need to mention number of rows but number of columns are necessary.
- char x[][10] - Two dimensional character array that contains array of strings.
- int n - Number of rows (strings).
Passing Two-D character array into the function in c programming
#include <stdio.h>
void dispStrings(char x[][10],int n)
{
int i=0;
for(i=0;i<n;i++)
{
printf("%s\n",x[i]);
}
}
int main()
{
char a[][10]={
"This",
"is",
"Mike"
};
dispStrings(a,3) ;
}
Output
This
is
Mike
Two dimensional character array declaration with initialization
Consider the following statement
char a[][10]={
"This",
"is",
"Mike"
};
Here we are initializing array a with the three strings "This", "is", "mike". In this declaration number char a[][10] - number of rows are three which is determining through initialization because there are three strings and number of columns are 10 that is the maximum value of a string.