Passing strings to a function

  • We can pass strings to a function in the same way we pass an array to a function.

Points to Remember:

  • A string is an array of characters terminated by ‘\0’.

  • All the characters of a string are stored in contiguous memory locations. 

  • Though scanf( ) can be used to receive multi-word strings, gets( ) can do the same job in a cleaner way. 

  • Both printf( ) and puts( ) can handle multi-word strings. 

  • Strings can be operated upon using several standard library functions like strlen( ), strcpy( ), strcat( ) and strcmp().

  • A 2-D array can be used to handle several strings, in practice an array of pointers to strings is preferred since it takes less space and is efficient in processing strings.

  • malloc( ) function can be used to allocate space in memory during execution of the program.