Functions with C-Style String Arguments

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);          // ghost is &ghost[0]int n2 = strlen(str);            // pointer to charint n3 = strlen("gamboling");    // address of string

Informally, you can say that you’re passing a string as an argument, but you’re really passing the address ...

Get C++ Primer Plus 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.