Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following snippet:

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 receive:

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

Could someone give me a hint at what's wrong?

like image 526
MrDuk Avatar asked Apr 24 '14 02:04

MrDuk


2 Answers

C++ does not perform implicit casts from void*, you must make this explicit

data = static_cast<char*>(mmap((caddr_t)0, bytesToTransfer, PROT_READ, MAP_SHARED, fd, 0));
like image 76
user657267 Avatar answered Nov 15 '22 02:11

user657267


mmap returns a void*. data is a char*. You'll need to cast it:

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

This will try to resolve the type issue at compile time.

like image 20
Jackson Avatar answered Nov 15 '22 01:11

Jackson