Search code examples
c++arrayspointersmemory-mapping

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,


Solution

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

    char* p = (char*)CreateFileMapping(...);
    p[123] = 'x';
    ...