Pass an array to a function - C Function

C examples for Function:Function Parameter

Introduction

The following program passes a character array to a function that calculates the length of the incoming string.

Demo Code

#include <stdio.h> 

int nameLength(char []); 

int main() { /*from   www .  ja  va2  s.c o m*/
   char aName[20] = {'\0'}; 
   printf("\nEnter your first name: "); 
   scanf("%s", aName); 
   printf("\nYour first name contains "); 
   printf("%d characters\n", nameLength(aName)); 
}
int nameLength(char name[]) 
{ 
   int x = 0; 
   while ( name[x] != '\0' ) 
      x++; 
   return x; 
}

Related Tutorials