C++ 从“const char*”到“unsigned char*”的无效转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34470754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:22:56  来源:igfitidea点击:

Invalid conversion from ‘const char*’ to ‘unsigned char*’

c++castingtype-conversion

提问by anupamD

A simple C++ code:

一个简单的 C++ 代码:

int main(){
unsigned char* t="123";
}

on compilation with g++ gives following error:

使用 g++ 编译时出现以下错误:

invalid conversion from ‘const char*' to ‘unsigned char*' [-fpermissive]

Why?

为什么?

回答by Vlad from Moscow

In C++ string literals have types of constant character arrays. For example string literal "123"has type const char[4].

在 C++ 中,字符串文字具有常量字符数组的类型。例如,字符串文字的"123"类型为const char[4]

In expressions with rare exceptions arrays are converted to pointers to their first elements.

在具有罕见异常的表达式中,数组被转换为指向它们的第一个元素的指针。

So in this declaration

所以在这个声明中

unsigned char* t="123";

the initializer has type const char *. There is no implicit conversion from const char *to unsigned char *

初始值设定项的类型为const char *。没有从const char *到的隐式转换unsigned char *

You could write

你可以写

const unsigned char* t = reinterpret_cast<const unsigned char *>( "123" );

回答by Davislor

Another approach, which gets you a modifiable unsigned chararray as you originally wanted, is:

另一种unsigned char为您提供最初想要的可修改数组的方法是:

#include <cstdlib>
#include <iostream>

using std::cout;
using std::endl;

int main()
{
    unsigned char ta[] = "123";
    unsigned char* t = ta;

    cout << t << endl;  // Or ta.

    return EXIT_SUCCESS;
}

You can add constto both declarations if you wish, to get const unsigned charwithout an explicit cast.

const如果您愿意,您可以添加到两个声明中,const unsigned char无需显式转换。

回答by SajithP

Conversions from one type to another type is easy when you use self-defined macros. So here is a set of macros you can use across any platform (Windows, Linux, Solaris, AIX etc...)

当您使用自定义宏时,从一种类型到另一种类型的转换很容易。所以这里有一组可以在任何平台(Windows、Linux、Solaris、AIX 等)上使用的宏

#define M_ToCharPtr(p)        reinterpret_cast<char*>(p)                   // Cast to char*
#define M_ToWCharPtr(p)       reinterpret_cast<wchar_t*>(p)                // Cast to wchar_t*
#define M_ToConstCharPtr(p)   reinterpret_cast<const char*>(p)             // Cast to const char*
#define M_ToConstWCharPtr(p)  reinterpret_cast<const wchar_t*>(p)          // Cast to const wchar_t*
#define M_ToUCharPtr(p)       reinterpret_cast<unsigned char*>(p)          // Cast to unsigned char*
#define M_ToConstUCharPtr(p)  reinterpret_cast<const unsigned char*>(p)    // Cast to const unsigned char*
#define M_ToUCharPtr(n)       reinterpret_cast<unsigned char*>(n)          // Cast to unsigned char*
#define M_ToVoidPtr(p)        reinterpret_cast<void*>(p)                   // Cast to void*
#define M_ToConstVoidPtr(p)   reinterpret_cast<const void*>(p)             // Cast to const void*
#define M_ToIntPtr(n)         reinterpret_cast<int*>(n)                    // Cast to int*
#define M_ToConstIntPtr(p)    reinterpret_cast<const int*>(p)              // Cast to const int*
#define M_ToDoublePtr(n)      reinterpret_cast<double*>(n)                 // Cast to double*
#define M_ToConstDoublePtr(n) reinterpret_cast<const double*>(n)           // Cast to const double*
#define M_ToBoolPtr(n)        reinterpret_cast<bool*>(n)                   // Cast to bool*
#define M_ToConstBoolPtr(n)   reinterpret_cast<const bool*>(n)             // Cast to const bool*

// General Cast
#define M_To(T, p)            reinterpret_cast<T>(p)                       // Cast to T

In your case

在你的情况下

const unsigned char* t = reinterpret_cast<const unsigned char *>("UCHAR TO CONST UCHAR");

is equivalent to

相当于

const unsigned char* t = M_ToConstUCharPtr("UCHAR TO CONST UCHAR");

回答by malang

Simply use

只需使用

  1. just char* in place of unsigned char* during declaration

  2. char t[MAX_SIZE] = "123"; // MAX_SIZE should be defined earlier

  3. time tested strcpy()and strncpyfunctions

  1. 在声明期间仅使用 char* 代替 unsigned char*

  2. 图表 t[MAX_SIZE] = "123"; // MAX_SIZE 应该更早定义

  3. 经过时间测试的strcpy()strncpy函数