简体   繁体   English

在C ++中将指针转换为数组

[英]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. CreateFileMapping函数返回一个指向内存映射文件的指针,我想将该内存映射视为一个数组。

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';
...

In C/C++, pointers and arrays are not the same thing. 在C / C ++中,指针和数组不是一回事。

But in your case, for your purposes they are. 但在你的情况下,为了你的目的,他们是。

You have a pointer. 你有一个指针。

You can give it a subscript. 你可以给它一个下标。

Eg a char* pointer points to the start of "hello" 例如,一个char *指针指向“你好”的开头

pointer[0] is the first character 'h' 指针[0]是第一个字符'h'

pointer[1] is the second character 'e' 指针[1]是第二个字符'e'

So just treat it as you are thinking about an array. 所以只要在考虑数组时就对待它。

"In C/C++, pointers and arrays are not the same thing." “在C / C ++中,指针和数组并不是一回事。” is true, but, the variable name for the array is the same as a pointer const (this is from my old Coriolis C++ Black Book as I recall). 是的,但是,数组的变量名与指针const相同(这是我记得的旧科里奥利C ++黑皮书)。 To wit: 以机智:

char carray[5];
char caarray2[5];
char* const cpc = carray;    //can change contents pointed to, but not where it points

/*
  cpc = carray2;    //NO!! compile error
  carray = carray2; //NO!! compile error - same issue, different error message
*/

cpc[3] = 'a';  //OK of course, why not.

Hope this helps. 希望这可以帮助。

But how's pointer different from array? 但是如何指针与数组不同? What's wrong with 怎么了?

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

You can treat the Array more or less like you would treat an array from now on. 您可以或多或少地对待Array就像从现在开始处理数组一样。

You can use a C-style cast: 你可以使用C风格的演员:

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

Or the preferred reinterpret cast: 或者首选的重新解释:

char *p std::reinterpret_cast<char*>(CreateFileMapping(...));
p[123] = 'x';

I was also searching for this answer. 我也在寻找这个答案。 What you need to do is to create your own type of array. 您需要做的是创建自己的数组类型。

    static const int TickerSize = 1000000;
    int TickerCount;
    typedef char TickerVectorDef[TickerSize];

You can also cast your pointer into this new type. 您还可以将指针转换为此新类型。 Otherwise you get "Compiler error C2440". 否则你会得到“编译器错误C2440”。 It has to be a fixed size array though. 它必须是固定大小的阵列。 If you only use it as a pointer, no actual memory is allocated (except 4-8 bytes for the pointer itself). 如果仅将其用作指针,则不会分配实际内存(指针本身除4-8个字节外)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM