473,609 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incrementing void *

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 ?

Mar 9 '07 #1
53 4783
su************* *@yahoo.com, India wrote:
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 isn't an lvalue, so you can not increment it.
Which is correct ?
ptr = ptr + sizeof(myStruct );

--
Ian Collins.
Mar 9 '07 #2
"Ian Collins" <ia******@hotma il.comwrote in message
news:55******** ******@mid.indi vidual.net...
su************* *@yahoo.com, India wrote:
>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(myStruc t);

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 isn't an lvalue, so you can not increment it.
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 );
Nonsense. You can't add anything to a void *.

The posed question doesn't answer the burning question,
"increment by what?". One possible sensible interpretation
is to undo the implicit void cast and write:

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

Yet another is:

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

These at least have the virtue of being well defined. But
then so does:

ptr = ++(char *)ptr);

so the problem is at least ambiguous. And we still don't
know what the function increment does.

I predict a very long, unimportant thread.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Mar 9 '07 #3
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.

Mar 9 '07 #4
P.J. Plauger wrote:
>>
(int*)ptr isn't an lvalue, so you can not increment it.


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.
Oops, I appear to have posted complete bollocks. Sorry.

--
Ian Collins.
Mar 9 '07 #5
su************* *@yahoo.com, India wrote:
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 );
None. The closest is choice two, depending on what the function
exactly does.
THEIR Correct Answer given in this site is: ptr = ptr +
sizeof(myStruct );
You cannot do any arithmetic on void pointers.
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.
But why a cast to int *? The more likely cast would be to myStruct *
or myStruct[10] *. Also you can use a cast to unsigned char * to
access the bytes of the array myArray.

Mar 9 '07 #6
su************* *@yahoo.com, India wrote:
I myself know that ++(int *)ptr is not correct.
It's not that it is not correct. It's just likely to be wrong.
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 would work only if myStruct's alignment requirements are only as
strict as for an int, something that's unlikely unless myStruct
happens to be an obfuscatory typedef for int.

I myself, if forced, would've chosen choice two.

Mar 9 '07 #7
P.J. Plauger wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:55******** ******@mid.indi vidual.net...
su************* *@yahoo.com, India wrote:
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 ?
ptr = ptr + sizeof(myStruct );

Nonsense. You can't add anything to a void *.

The posed question doesn't answer the burning question,
"increment by what?". One possible sensible interpretation
is to undo the implicit void cast and write:

ptr = ++((myStruct *)ptr);
Shouldn't this be:

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

and similarly for the others?

Mar 9 '07 #8
"P.J. Plauger" <pj*@dinkumware .comwrites:
"Ian Collins" <ia******@hotma il.comwrote in message
news:55******** ******@mid.indi vidual.net...
>su************* *@yahoo.com, India wrote:
>>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(myStru ct);

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
incrementin g.
(int*)ptr isn't an lvalue, so you can not increment it.

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.
Um, are you sure you're not thinking of the C++ rules?

The result of a cast is not an lvalue, and the argument to "++" must
be an lvalue, so Choice 3 is a constraint violation. (Both gcc and
Sun's C compiler complain about it; that doesn't prove anything, but
it does bolster my confidence.) You can add 1 to it, but you can't
increment it with "++".

(Choices 1, 4, and 5 are also constraint violations, since addition
isn't defined for void*. Choice 2 *could* be correct if "increment"
were a carefully crafted macro, but I don't think that's the intent.)
>>Which is correct ?
>ptr = ptr + sizeof(myStruct );

Nonsense. You can't add anything to a void *.

The posed question doesn't answer the burning question,
"increment by what?". One possible sensible interpretation
is to undo the implicit void cast and write:

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

Yet another is:

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

These at least have the virtue of being well defined. But
then so does:

ptr = ++(char *)ptr);

so the problem is at least ambiguous. And we still don't
know what the function increment does.
All three of these, if a cast yielded an lvalue, would modify ptr
twice between sequence points and therefore invoke undefined behavior
(<PICKY>and the last one has a missing parenthesis</PICKY>). Changing
"++" to "1 + " should fix that.

I understand that *some* casts in C++ yield lvalues, but I don't know
the rules. Perhaps the question on brainbench.com was actually about
C++? Or maybe it was just wrong.

I'm hesitant to take the risk of disagreeing with P.J. Plauger, but
either I'm right or I'm about to learn something.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 9 '07 #9
"santosh" <sa*********@gm ail.comwrites:
su************* *@yahoo.com, India wrote:
>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 );

None. The closest is choice two, depending on what the function
exactly does.
[...]

If increment is a function, it can't modify ptr. It could be a macro,
though.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
8941
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in advance. Erik
4
1822
by: masterpaladin38 | last post by:
I've been working on this problem for some time and I must say I'm stump. Any help would be appreciated. Basically what I'm trying to do is write the results of a loop to a new text file with every pass. For example the user chooses that the loop should run 10 times. I want to write the results of each pass of the loop to a text file by incrementing the filename and creating a new text file. So if it the loop went through 10 passes I...
9
3362
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to array of int */\ ap = &a1; printf("%d\n", **ap);
0
8095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8556
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8410
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6068
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5526
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1690
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.