ADVERTISEMENT
ADVERTISEMENT

C program to pass an array of strings to a function

In this C program, we are going to learn how to pass a two dimensional character array (array of strings) to a user defined function?
Submitted by IncludeHelp, on March 20, 2018

Given an array of strings and we have to pass to a user define function and print the strings in the function using C program.

Here is the function that we have used in the program,

void Strfun(char **ptr , int count)

Here,

  • void is the returns type of the function i.e. it will return nothing.
  • Strfun is the name of the function.
  • char **ptr is the pointer to character pointer i.e. to store the address of a two dimensional character array (known as array of strings).
  • int count is the total number of strings.

Function calling statement,

Strfun(buff,3);

Here,

  1. buff is the array of strings.
  2. 3 is the total number of strings.

Program to pass an array of strings to a function in C

/*  
C program to array of strings to a function
*/

#include <stdio.h>

// function to print strings
void Strfun(char **ptr , int count)
{
    int i=0;
    
    for(i=0;i<count;i++)
    {
        printf("\nString [%d] : %s",i,ptr[i]);  fflush(stdout);
    }
    
}

// main function
int main()
{ 
   // Declare an array of pointers and assign some strings into it
   char *buff[4] = {"Hello function" , "How are you?" , "Catch some strings"};
	
   // Passing array of strings to Function
   Strfun(buff,3); // here 3 is the number of strings
   return 0;
}

Output

String [0] : Hello function
String [1] : How are you?
String [2] : Catch some strings

C User-defined Functions Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.