简体   繁体   中英

C++ mixing pointers to different char types

I am writing a program in C++ that takes an ACT (Adobe Color Table) file and converts it to a plain-text readable JASC-PAL file. I want to read the binary data from the ACT file and store it in memory to use. I wrote the following code to do that and it builds using BCC55. The problem is I get a build warning: "Warning W8079 : Mixing pointers to different 'char' types in function read_file()".

unsigned char * memblock;

bool read_file()
{
    int filesize;
    ifstream act ("test.act", ios::binary|ios::ate);
    if (act.is_open())
    {
        filesize = act.tellg();
        act.seekg(0);
        memblock = new unsigned char [filesize];
        act.read(memblock, filesize);
        act.close();
        cout << "Color Table loaded to memory." << endl;
        return true;
    }
    else
    {
        cout << "Failed to open file." << endl;
        return false;
    }
}

Looking this warning up at the Embarcadero documentation wiki it seems to be because I am passing a unsigned char pointer to a function expecting a regular char pointer. It says this is technically incorrect but usually harmless. My question is if this is, strictly speaking, incorrect then how do I go about doing this without causing the W8079 warning at build time? Should I even bother with this since the warning is harmless and my code works as expected?

Add a cast. In this case I would just use a simple C cast.

act.read((char*)memblock, filesize);

but you could use reinterpret_cast as well

act.read(reinterpret_cast<char*>(memblock), filesize);

but this makes it seem a bigger deal than it is. As the documentation says, this is harmless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM