增加空白* [英] incrementing void *

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

问题描述

我在 www.brainbench.com 上看到了这个问题

void * ptr;

myStruct myArray [10];


ptr = myArray;


以下哪一项是增加变量的正确方法

" ptr"?

选择1 ptr = ptr + sizeof(ptr);

选择2增量(ptr);

选择3 ++(int *)ptr;

选择4 ptr = ptr + sizeof(myArray);

选择5 ptr = ptr + sizeof(myStruct);


他们在本网站给出的正确答案是:ptr = ptr +

sizeof (myStruct);


但我的答案是++(int *)ptr。原因是ptr无效*,我们

不能添加任何数字。它必须在

递增之前转换为有效对象。


哪个是正确的?

I saw this question from www.brainbench.com

void *ptr;
myStruct myArray[10];

ptr = myArray;

Which of the following is the correct way to increment the variable
"ptr"?
Choice 1 ptr = ptr + sizeof(ptr);
Choice 2 increment(ptr);
Choice 3 ++(int*)ptr;
Choice 4 ptr = ptr + sizeof(myArray);
Choice 5 ptr = ptr + sizeof(myStruct);

THEIR Correct Answer given in this site is: ptr = ptr +
sizeof(myStruct);

But my answer is ++(int*)ptr. The reason is that ptr being void *, we
cannot add any number to it. It must be cast to a valid object before
incrementing.

Which is correct ?

推荐答案

su *********** ***@yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:

我从 www.brainbench.com

void * ptr;

myStruct myArray [10] ;


ptr = myArray;


以下哪项是增加变量的正确方法

" ; ptr"?

选择1 ptr = ptr + sizeof(ptr);

选择2增量(ptr);

选择3 ++ (int *)ptr;

选择4 ptr = ptr + sizeof(myArray);

选择5 ptr = ptr + sizeof(myStruct);


他们在本网站给出的正确答案是:ptr = ptr +

sizeof(myStruct);


但我的答案是++ (INT *)PTR。原因是ptr无效*,我们

不能添加任何数字。它必须在

递增之前强制转换为有效对象。
I saw this question from www.brainbench.com

void *ptr;
myStruct myArray[10];

ptr = myArray;

Which of the following is the correct way to increment the variable
"ptr"?
Choice 1 ptr = ptr + sizeof(ptr);
Choice 2 increment(ptr);
Choice 3 ++(int*)ptr;
Choice 4 ptr = ptr + sizeof(myArray);
Choice 5 ptr = ptr + sizeof(myStruct);

THEIR Correct Answer given in this site is: ptr = ptr +
sizeof(myStruct);

But my answer is ++(int*)ptr. The reason is that ptr being void *, we
cannot add any number to it. It must be cast to a valid object before
incrementing.



(int *)ptr不是左值,所以你可以不要增加它。

(int*)ptr isn''t an lvalue, so you can not increment it.


哪个是正确的?
Which is correct ?



ptr = ptr + sizeof(myStruct);


-

Ian Collins。

ptr = ptr + sizeof(myStruct);

--
Ian Collins.


" Ian Collins" < ia ****** @ hotmail.comwrote in message

news:55 ************** @ mid.individual.net ...
"Ian Collins" <ia******@hotmail.comwrote in message
news:55**************@mid.individual.net...
su ********** ****@yahoo.com ,印度写道:
su**************@yahoo.com, India wrote:

>我从 www.brainbench.com

void * ptr;
myStruct myArray [10];

ptr = myArray;

以下哪一项是增加变量的正确方法
" ptr"
选择1 ptr = ptr + sizeof(ptr );
选择2增量(ptr);
选择3 ++(int *)ptr;
选择4 ptr = ptr + sizeof(myArray);
选择5 ptr = ptr + sizeof(myStruct);

他们在本网站给出的正确答案是:ptr = ptr +
sizeof(myStruct);

但我的回答是+ +(INT *)PTR。原因是ptr无效*,我们
不能添加任何数字。它必须在增量之前强制转换为有效对象。
>I saw this question from www.brainbench.com

void *ptr;
myStruct myArray[10];

ptr = myArray;

Which of the following is the correct way to increment the variable
"ptr"?
Choice 1 ptr = ptr + sizeof(ptr);
Choice 2 increment(ptr);
Choice 3 ++(int*)ptr;
Choice 4 ptr = ptr + sizeof(myArray);
Choice 5 ptr = ptr + sizeof(myStruct);

THEIR Correct Answer given in this site is: ptr = ptr +
sizeof(myStruct);

But my answer is ++(int*)ptr. The reason is that ptr being void *, we
cannot add any number to it. It must be cast to a valid object before
incrementing.



(int *)ptr不是左值,所以你不能递增它。

(int*)ptr isn''t an lvalue, so you can not increment it.



废话。如果

myStruct没有至少与

int一样强大的存储边界(或者如果你在机器上),那么递增(int *)ptr会有危险甚至十个MyStruct都比单个int小一些b $ b。但是lvaleness无事可做

用它。

Nonsense. There are dangers in incrementing (int *)ptr if
myStruct is not on at least as strong a storage boundary as
int (or if you''re on a machine where even ten MyStruct are
smaller than a single int). But lvaleness has nothing to do
with it.


>哪个是正确的?
>Which is correct ?


ptr = ptr + sizeof(myStruct);
ptr = ptr + sizeof(myStruct);



废话。你不能在虚空中添加任何东西*。


提出的问题并没有回答这个问题,

按什么增加? " ;.一个可能明智的解释

是撤消隐式空洞演员并写:


ptr = ++((myStruct *)ptr);


还有一个是:


ptr = ++((myStruct [10] *)ptr);


这些至少具有明确定义的优点。但是

那么这样做:


ptr = ++(char *)ptr);


所以问题至少是模棱两可的。我们仍然不知道
知道函数增量的作用。


我预测一个非常长的,不重要的线程。

PJ Plauger

Dinkumware,Ltd。
http ://www.dinkumware.com


我自己知道++(int *)ptr不正确。但是在给出的五个b $ b选项中,如果必须选择一个选项,

只有++(int *)ptr可以递增,因为只有在这种情况下,对象

尺寸用于增量。

I myself know that ++(int *)ptr is not correct. But among the five
choices given and if one choice has to be selected,
only ++(int *)ptr can be incremented because only in this case, object
size is known for doing the incrementation.


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

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