search

Home  >  Q&A  >  body text

C++中const char[] 为什么不能用strlen()?

如题所示,为什么字符串加了const就会报错,去掉const就能过?

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
    const char letter[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";

    cout<<strlen(letter)<<endl;

    return 0;
}
迷茫迷茫2804 days ago968

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 15:09:58

    Post the error message and take a look. It is normal to execute the following command under Mac and Centos after compilation

    g++ test.cpp -o test
    ./test

    reply
    0
  • 阿神

    阿神2017-04-17 15:09:58

    What compiler are you using? GCC 6.2 works fine.

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:09:58

    Thank you everyone, I made a mistake in the main function. I created two new main functions in a project, which caused this error. Thank you very much

    reply
    0
  • Cancelreply