c - How do I pass an anonymous string literal to a function? -


since string literal can initialized like

char mystring [] = "somestring"; 

it seems logical able use function like

int stringlength ( char * str ) {    char * c2 = str;    while (*c2++);    return (c2-str); } 

and call

printf("%d",stringlength("somestring")); 

however, error when that.

is there way can "cast" "something" proper input function, or have use line like

char str [] = "somestring"; printf("%d",stringlength(str));     

or there else should doing altogether?

char mystring [] = "somestring"; 

creates array of characters modifiable.

when use

printf("%d",stringlength("somestring")); 

it analogous to:

char const* temp = "somestring"; printf("%d",stringlength(temp)); 

that's why compiler not it.

you can change function use char const* argument able use that.

int stringlength ( char const* str ) {    char const* c2 = str;    while (*c2++);    return (c2-str); }