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

查看:183
本文介绍了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;
}

解决方案

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

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

这篇关于C++中const char[] 为什么不能用strlen()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆