Functions and C-Style Strings

A C-style string, you recall, consists of a series of characters terminated by the null character. Much of what you've learned about designing array functions applies to string functions, too. But there are a few special twists to strings that we unravel now.

Suppose you want to pass a string as an argument to a function. You have three choices for representing a string:

  • An array of char

  • A quoted string constant (also called a string literal)

  • A pointer-to-char set to the address of a string

All three choices, however, are type pointer-to-char (more concisely, type char *), so you can use all three as arguments to string-processing functions:

 char ghost[15] = "galloping"; char * str = "galumphing"; int n1 = strlen(ghost); ...

Get The Waite Group's C++ Primer Plus, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.