指针增量 [英] Pointer Increment

查看:129
本文介绍了指针增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


哪个是正确的?


假设我们为100个字节分配内存,


int * PtrMemoryBlock =(int *)malloc(100);


如果我这样做


PtrMemoryBlock = PtrMemoryBlock + 10;


它会指向第10个字节的内存loaction,由

malloc分配的memro或它将指向10 + 100(Byte Allocated by malloc)

记忆力?


提前致谢

Ranjeet

解决方案

ra *********** @ gmail.com 写道:

哪个是正确的?


和往常一样。

假设我们为100字节分配内存,

int * PtrMemoryBlock =(int *)malloc的(100);


这不是调用malloc()的正确方法。

如果我这样做

PtrMemoryBlock = PtrMemoryBlock + 10; <它会指向内存loaction的第10个字节,由malloc分配的memro或者它会指向10 + 100(由malloc分配的字节)
内存loaction?



不会。它会指向第10个int。


Richard



ra***********@gmail.com 写道:

亲爱的所有,

哪个是正确的?

让我们假设我们为100字节分配内存,

/> int * PtrMemoryBlock =(int *)malloc(100);

如果我这样做

PtrMemoryBlock = PtrMemoryBlock + 10;

会不会指向第10个字节的内存loaction,由malloc分配的memro或它将指向10 + 100(由malloc分配的字节)
内存loaction?

它将指向第10个整数loc通货膨胀。 int * PtrMemoryBlock =(int *)malloc(100);
假设PtrMemoryBlock = 0. PtrMemoryBlock = PtrMemoryBlock + 10;
经过上述操作后,PtrMemoryBlock将为40.

10 * 4 =第40个字节位置。

4是整数的大小。

10是增量值

提前致谢
Ranjeet




ranjeet.gu ... @ gmail.com写道:

它会指向第10个字节的内存loaction,由malloc分配的memro或者它将指向10 + 100(由malloc分配的字节)
内存loaction?



两者都没有,

它指向内存(PtrMemoryBlock只是指向)第N个的确定

byte,

和,N = 10 * sizeof(int)。

pointer" PtrMemoryBlock"声明指向int类型,所以当

对指针添加10时,在* / b $ b事实中添加了10 * sizeof(int)。


Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?

Thanks in advance
Ranjeet

解决方案

ra***********@gmail.com wrote:

Which is correct ?
Neither, as usual.
Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);
This is not the right way to call malloc().
If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?



No. It will point to the 10th int.

Richard



ra***********@gmail.com wrote:

Dear All,

Which is correct ?

Let suppose we allocate the memory for the 100 bytes,

int *PtrMemoryBlock = (int *)malloc(100);

If I do

PtrMemoryBlock = PtrMemoryBlock + 10;

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?
It will point to 10th integer location. int *PtrMemoryBlock = (int *)malloc(100); Assume PtrMemoryBlock = 0. PtrMemoryBlock = PtrMemoryBlock + 10; After the above operation PtrMemoryBlock will be 40.
10 * 4 = 40th byte location.
4 is the size of the integer.
10 is the increment value
Thanks in advance
Ranjeet




ranjeet.gu...@gmail.com write:

will it point to memory loaction of 10th byte, memroy allocated by
malloc OR it will point to the 10 + 100 (Byte Allocated by malloc)
memory loaction ?


neither ,
it points to memory (PtrMemoryBlock just points to) loaction of Nth
byte,
and, N = 10 * sizeof(int).
pointer "PtrMemoryBlock" is declared to point to int type, so while
doing an addition of 10 to the pointer , 10 * sizeof(int) is added in
fact.


这篇关于指针增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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