首頁  >  問答  >  主體

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;
}
迷茫迷茫2714 天前906

全部回覆(3)我來回復

  • ringa_lee

    ringa_lee2017-04-17 15:09:58

    把錯誤訊息貼出來看看,在Mac和Centos下執行下面指令編譯後執行是正常的

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

    回覆
    0
  • 阿神

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

    你用的什麼編譯器。 GCC 6.2正常。

    回覆
    0
  • 迷茫

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

    謝謝各位了,是我搞錯了主函數,我在一個專案裡新建了兩個主函數,導致了這個錯誤。非常感謝

    回覆
    0
  • 取消回覆