Incorrect conversion from 'void *' to 'char *' when using mmap ()

I have the following:

    char* filename;
    unsigned long long int bytesToTransfer;
int fd, pagesize;
char *data;

fd = open(filename, O_RDONLY);
if (fd==NULL) {fputs ("File error",stderr); exit (1);}

cout << "File Open: " << filename << endl;

pagesize = getpagesize();
data = mmap((caddr_t)0, bytesToTransfer, PROT_READ, MAP_SHARED, fd, 0);
if (*data == -1) {fputs ("Memory error",stderr); exit (2);}

cout << "Data to Send: " << data << endl;

But when I compile, I get:

error: incorrect conversion from 'void * to' char * [-fpermissive] data = mmap ((caddr_t) 0, bytesToTransfer, PROT_READ, MAP_SHARED, fd, 0);

Can someone give me a hint about what happened?

+4
source share
2 answers

C ++ does not execute implicit casts from void*, you have to make it explicit

data = static_cast<char*>(mmap((caddr_t)0, bytesToTransfer, PROT_READ, MAP_SHARED, fd, 0));
+4
source

mmap returns void *. data is char *. You need to drop it:

data = static_cast<char*>( mmap((caddr_t)0, bytesToTransfer, PROT_READ, MAP_SHARED, fd, 0) );

This will try to solve the type problem at compile time.

+2
source

Source: https://habr.com/ru/post/1537813/


All Articles