[Solved] How is a string declared as a parameter to a function in c?


In C, a string is a sequence of character values including a zero-valued terminator – the string "hello" is represented as the sequence {'h', 'e', 'l','l', 'o', 0}.

Strings (including string literals) are stored in arrays of character type:

char str[] = "hello";

Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration (like above), an expression of type “N-element array of T” will be converted (“decay”) to an expression of type “pointer to T” and the value of the expression will be the address of the first element of the array.

So if you call a function like

foo( str );

what the function actually receives is a char *, not an array of char, so the prototype will be

void foo( char * );           // declaration
void foo( char *str ) { ... } // definition

In the context of a function parameter declaration, T a[N] and T a[] are adjusted to T *a, so you could also write the prototype as

void foo( char str[] )

or

void foo( char str[N] )

but they’ll both be interpreted as

void foo( char *str )

solved How is a string declared as a parameter to a function in c?