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

浏览:45日期:2023-04-02

问题描述

如题所示,为什么字符串加了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;}

问题解答

回答1:

把错误信息贴出来看看,在Mac和Centos下执行下面命令编译后执行是正常的

g++ test.cpp -o test./test回答2:

你用的什么编译器。GCC 6.2正常。

回答3:

谢谢各位了,是我搞错了主函数,我在一个项目里新建了两个主函数,导致了这个错误。非常感谢

相关文章: