C语言 偏移指针的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3872704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:37:22  来源:igfitidea点击:

What is the correct way to offset a pointer?

carrayspointersoffset

提问by PICyourBrain

I want to pass a pointer to a function. I want this pointer to point to some place in the middle of an array. Say I have an array like such unsigned char BufferData[5000];, would the following statement be correct syntactically?

我想传递一个指向函数的指针。我希望这个指针指向数组中间的某个地方。假设我有一个这样的数组unsigned char BufferData[5000];,以下语句在语法上是否正确?

writeSECTOR( destAddress, (char *)( BufferData + (int)(i * 512 )) );
// destAddress is of type unsigned long
// writeSECTOR prototype: int writeSECTOR ( unsigned long a, char * p );
// i is an int

回答by nos

That would do, but just make it:

那样做,但只要做到:

 writeSECTOR( destAddress, &BufferData[i * 512]);

(It sounds like writeSECTOR really should take an unsigned char* though)

(听起来 writeSECTOR 确实应该采用 unsigned char* )

回答by reko_t

You can just do BufferData + i * 512. An arithmetic +operator on char* yields a char* when you add an integer value to it.

你可以做BufferData + i * 512+当您向其添加整数值时,char* 上的算术运算符会产生一个 char*。

回答by locka

Pointer arithmetic is fairly simple to understand. If you have a pointer to the first element of an array then p + 1 points to the second element and so on regardless of size of each element. So even if you had an array of ints, or an arbitrary structure MyData it would hold true.

指针运算相当容易理解。如果您有一个指向数组第一个元素的指针,则 p + 1 指向第二个元素,依此类推,无论每个元素的大小如何。因此,即使您有一个整数数组或任意结构 MyData,它也会成立。

MyData data[100];
MyData *p1 = data;;  // same as &data[0]
MyData *p2 = p1 + 1; // same as &data[1]
MyData *p3 = p2 + 1; // same as &data[2]
MyData *p4 = p2 - 1; // same as &data[0] again

If your array is unsigned char then you just add however many bytes offset you wish to go into the array, e.g.

如果您的数组是无符号字符,那么您只需添加您希望进入数组的字节偏移量,例如

unsigned char data[16384];
unsigned char *offset = data + 512; // same as &data[512]
*offset = 5; // same as data[512] = 5;

Alternatively if the notation is confusing, you can always refer to it as it is shown in comments above, e.g. &data[512]

或者,如果符号令人困惑,您可以随时参考上面的评论中显示的内容,例如 &data[512]

回答by Nathan Fellman

That should work. In C, adding an integer to a pointer increases the pointer by the integer multiplied by the sizeofthe type the pointer points to.

那应该工作。在 C 中,向指针添加整数会增加指针乘以整数乘以sizeof指针指向的类型。

回答by Jose

it looks ok, but try it on the compiler,

看起来不错,但在编译器上试试,

You can use writeSECTOR( destAddress, &BufferData[i * 512] );

您可以使用 writeSECTOR( destAddress, &BufferData[i * 512] );

回答by T.E.D.

That looks like it will pass in the address of the i*512'th element of the array. Is that what you want it to do?

看起来它会传入数组的第 i*512 个元素的地址。这就是你想要它做的吗?

I don't really see what that cast to int is buying you though.

不过,我真的不明白对 int 的强制转换会为您购买什么。

回答by shenju

That should work as you think. Pointer in C is just an address in RAM,so you could drift the pointer in your measure.

这应该像你想的那样工作。C 中的指针只是 RAM 中的一个地址,因此您可以在度量中漂移指针。