Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a pointer to an array in C++

The CreateFileMapping function returns a pointer to a memory mapped file, and I want to treat that memory mapping as an array.

Here's what I basically want to do:

char Array[] = (char*) CreateFileMapping(...);

Except apparently I can't simply wave my arms and declare that a pointer is now an array.

Do you guys have any idea how I can do this? I don't want to copy the the values the pointer is pointing to into the array because that will use too much memory with large files.

Thanks a bunch,

like image 243
Zain Rizvi Avatar asked Aug 09 '26 12:08

Zain Rizvi


1 Answers

You do not need to. You can index a pointer as if it was an array:

char* p = (char*)CreateFileMapping(...);
p[123] = 'x';
...
like image 150
Pavel Minaev Avatar answered Aug 11 '26 02:08

Pavel Minaev