将指针转换为C ++中的数组 [英] Convert a pointer to an array in C++

查看:154
本文介绍了将指针转换为C ++中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CreateFileMapping函数返回一个指向内存映射文件的指针,我想将该内存映射视为一个数组。



这里是我基本想要做的:

  char Array [] =(char *)CreateFileMapping 

除了显然我不能简单地挥动我的手臂,并声明一个指针现在是一个数组。 / p>

你们有什么想法我该怎么做吗?我不想将指针指向的值复制到数组中,因为这将对大文件使用太多的内存。



感谢一堆, p>

解决方案

您不需要。您可以将指针索引为一个数组:

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


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,

解决方案

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

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

这篇关于将指针转换为C ++中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆