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 »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.