Hi, does anyone know why I can use 'sizeof()' but not 'strlen()' for defining the size of an array. I get the following error message in the picture I've attached:
Thanks in advance.
Hi, does anyone know why I can use 'sizeof()' but not 'strlen()' for defining the size of an array. I get the following error message in the picture I've attached:
Thanks in advance.
Because sizeof() is evaluated at compile time, whereas strlen() is evaluated at run-time.
Also, only C99 supports variable length arrays of the form you're using.
What you need to write in C++ is
char *asterisk = new char[ strlen(s) + 1 ];
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.