Is it possible to use strlen while declaring a new string?


char a[]="Hello world";
char b[strlen(a)]; // alloc memory to string b equal to the string length of a

If not, then what is the solution if I don't want to directly enter the length of another string?

And I'm unable to use the strrev function. I get the following error

error-implicit declaration of function 'strrev' is invalid in C99

How to fix this?


Yes, it is possible. But you should add an extra space for the null character if you want b to be exactly the same that a.

char a[] = "Hello world";
char b[strlen(a) + 1];

I think strrev() is not included in any standard c library.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .