445,918 Members | 2,258 Online
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

# incrementing void *

 P: n/a 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 Replies

 P: n/a 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

 P: n/a "Ian Collins" I saw this question from www.brainbench.comvoid *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 *, wecannot add any number to it. It must be cast to a valid object beforeincrementing. (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

 P: n/a 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: n/a 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

 P: n/a 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

 P: n/a 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: n/a P.J. Plauger wrote: "Ian Collins"

 P: n/a "P.J. Plauger" su**************@yahoo.com, India wrote: >>I saw this question from www.brainbench.comvoid *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 *, wecannot add any number to it. It must be cast to a valid object beforeincrementing. (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 (and the last one has a missing parenthesis). 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_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Mar 9 '07 #9

 P: n/a "santosh" I saw this question from www.brainbench.comvoid *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_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Mar 9 '07 #10

 P: n/a "Keith Thompson" Nonsense. There are dangers in incrementing (int *)ptr ifmyStruct is not on at least as strong a storage boundary asint (or if you're on a machine where even ten MyStruct aresmaller than a single int). But lvaleness has nothing to dowith 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 "++". it doesnt work in C++ either. +1 would be best Mar 9 '07 #11

 P: n/a In article , P.J. Plauger >But my answer is ++(int*)ptr. The reason is that ptr being void *, wecannot add any number to it. It must be cast to a valid object beforeincrementing. >(int*)ptr isn't an lvalue, so you can not increment it. >Nonsense. There are dangers in incrementing (int *)ptr ifmyStruct is not on at least as strong a storage boundary asint (or if you're on a machine where even ten MyStruct aresmaller than a single int). But lvaleness has nothing to dowith it. You can add 1 to (int *)ptr, but you can't assign the result to it, because (int *)ptr is indeed not an lvalue (since some draft of C89). You can do ptr = (int *)ptr + 1 which converts the value back to void * before assigning it to plain uncast ptr. -- Richard >The posed question doesn't answer the burning question,"increment by what?". One possible sensible interpretationis to undo the implicit void cast and write: ptr = ++((myStruct *)ptr); I think you're temporarily confused. You can't apply ++ to cast expressions in standard C. -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963. Mar 9 '07 #12

 P: n/a In article , Keith Thompson The result of a cast is not an lvalue, and the argument to "++" mustbe an lvalue, so Choice 3 is a constraint violation. (Both gcc andSun's C compiler complain about it; that doesn't prove anything, butit does bolster my confidence.) gcc has traditionally allowed this as an extension; gcc 4.0 reports warning: target of assignment not really an lvalue; this will be a hard error in the future -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963. Mar 9 '07 #13

 P: n/a Keith Thompson wrote: "santosh"

 P: n/a On Mar 9, 11:02 am, "santosh" I saw this question fromwww.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. Yes, careless of me. It's unlikely to be a macro though, given that it's in lower case There are several lowercase macros in the standard; stdin (as long as

 P: n/a 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 ? Unless I'm very much mistaken, all five are wrong. 1) Pointer arithmetic is not defined for pointers to incomplete types. (6.5.6/2) 2) The C language has no keyword, built-in function, or macro named `increment'. 3) The `++' operator requires a modifiable lvalue as its operand, and `(int*)ptr' is not a modifiable lvalue. (6.5.3.1/1, 6.3.2.1/1) 4) See 1. 5) See 1. A possible quibble could be advanced in favor of 2. One could make the answer correct (for some definition of "correct") by introducing an `increment' macro that would in some sense "increment" its operand. That's trick question territory, though, and beneath contempt. -- Eric Sosman es*****@acm-dot-org.invalid Mar 9 '07 #16

 P: n/a On Mar 9, 12:43 am, "subramanian10...@yahoo.com, India"

 P: n/a AdrianH wrote: On Mar 9, 12:43 am, "subramanian10...@yahoo.com, India" Choice 3 ++(int*)ptr; I've also seen people use this trick, though how portable it is I'm not sure due to the possiblity that a pointer to different types may not necessarily be the same (in size or otherwise). typedef union { void * vp; int * ip; } p; p ptr = ; // assigned to void * ++p.ip; Adrian Mar 9 '07 #18

 P: n/a Adrian Hawryluk said: AdrianH wrote: >On Mar 9, 12:43 am, "subramanian10...@yahoo.com, India"Choice 3 ++(int*)ptr; I've also seen people use this trick, though how portable it is I'm not sure due to the possiblity that a pointer to different types may not necessarily be the same (in size or otherwise). typedef union { void * vp; int * ip; } p; p ptr = ; // assigned to void * ++p.ip; This will indeed fail on implementations where the representations of void * and int * differ. For example, to quote the FAQ: "Some 64-bit Cray machines represent int * in the lower 48 bits of a word; char * additionally uses some of the upper 16 bits to indicate a byte address within a word." -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Mar 9 '07 #19

 P: n/a su**************@yahoo.com 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 ? How about something along the lines of: ptr = &myArray[j++]; -- Bill C. "I am NOT lost.... I'm *exploring*" Mar 9 '07 #20

 P: n/a spacecriter (Bill C) wrote: su**************@yahoo.com wrote: >I saw this question from www.brainbench.comvoid *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 *, wecannot add any number to it. It must be cast to a valid object beforeincrementing.Which is correct ? How about something along the lines of: ptr = &myArray[j++]; What about it? -- Adrian Mar 9 '07 #21

 P: n/a Adrian Hawryluk wrote: spacecriter (Bill C) wrote: >su**************@yahoo.com wrote: >>I saw this question from www.brainbench.comvoid *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 objectbefore incrementing.Which is correct ? How about something along the lines of:ptr = &myArray[j++]; What about it? I meant to say that this will produce the desired effect (implied by their "correct" option number 5) without operating on the poiner directly. -- Bill C. "I am NOT lost.... I'm *exploring*" '01 FXDXT '94 Shadow VLX yank "yercrank" to e-mail Mar 9 '07 #22

 P: n/a Richard Tobin wrote: In article , P.J. Plauger >>>But my answer is ++(int*)ptr. The reason is that ptr being void *, wecannot add any number to it. It must be cast to a valid object beforeincrementing. >>>(int*)ptr isn't an lvalue, so you can not increment it. >>Nonsense. There are dangers in incrementing (int *)ptr ifmyStruct is not on at least as strong a storage boundary asint (or if you're on a machine where even ten MyStruct aresmaller than a single int). But lvaleness has nothing to dowith it. You can add 1 to (int *)ptr, but you can't assign the result to it, because (int *)ptr is indeed not an lvalue (since some draft of C89). You can do ptr = (int *)ptr + 1 which converts the value back to void * before assigning it to plain uncast ptr. >>The posed question doesn't answer the burning question,"increment by what?". One possible sensible interpretationis to undo the implicit void cast and write: ptr = ++((myStruct *)ptr); I think you're temporarily confused. You can't apply ++ to cast expressions in standard C. Thank you Richard, so I didn't post total bollocks after all. -- Ian Collins. Mar 9 '07 #23

 P: n/a spacecriter (Bill C) wrote: Adrian Hawryluk wrote: >spacecriter (Bill C) wrote: >>su**************@yahoo.com wrote:I saw this question from www.brainbench.comvoid *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 objectbefore incrementing.Which is correct ?How about something along the lines of:ptr = &myArray[j++]; What about it? I meant to say that this will produce the desired effect (implied by their "correct" option number 5) without operating on the poiner directly. It looks correct to me. It is just like saying: prt = &myArray[j]; j += 1; Personally, I don't see any reason nowadays for the ++/-- pre/post operators. Yeah, their somewhat covenant, but they can make code hard to read. I think the comma operator is more understandable as the sequence point is *right* there. Adrian Mar 10 '07 #24

 P: n/a "Ian Collins" In article ,P.J. Plauger >>>>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 ifmyStruct is not on at least as strong a storage boundary asint (or if you're on a machine where even ten MyStruct aresmaller than a single int). But lvaleness has nothing to dowith it. You can add 1 to (int *)ptr, but you can't assign the result toit, because (int *)ptr is indeed not an lvalue (since some draftof C89).You can do ptr = (int *)ptr + 1 which converts the value backto void * before assigning it to plain uncast ptr. >>>The posed question doesn't answer the burning question,"increment by what?". One possible sensible interpretationis to undo the implicit void cast and write: ptr = ++((myStruct *)ptr); I think you're temporarily confused. You can't apply ++ to castexpressions in standard C. Yep. Been writing too much C++ lately. The irony is that I'm the guy who insisted that ++x be an rvalue in C89. Thank you Richard, so I didn't post total bollocks after all. But I contributed at least a partial version of same. Thanks for the re-education. P.J. Plauger Dinkumware, Ltd. http://www.dinkumware.com Mar 10 '07 #25

 P: n/a 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 = (char *)ptr + sizeof *myArray; -- pete Mar 11 '07 #27

 P: n/a On Mar 9, 6:51 pm, "P.J. Plauger" ptr = ++((myStruct[10] *)ptr); These at least have the virtue of being well defined. As well as what has already been mentioned, this attempted cast is a syntax error. I guess you mean (mystruct (*)[10]). Mar 11 '07 #28

 P: n/a Adrian Hawryluk "Adrian Hawryluk" >Personally, I don't see any reason nowadays for the ++/-- pre/postoperators. Yeah, their somewhat covenant, but they can make codehard to read. I think the comma operator is more understandable asthe sequence point is *right* there. Preincrement can go.Postincrement is idiomatic. It means "do something to this, then goon to the next one". Still leads to bad code. Take for instance: x = y++ + y++; That has no sequence point between the y++s. x could equal to (y+1)+(y+1) or (y+1)+(y+2). This goes for all operators except comma ',' which is defined as a sequence point. Though I guess the same could be said for: x = (y+=1) + (y+=1); and other variants. The best thing to do is just not use operators with side-effects inside an equation. The best thing to do is to learn the rules and not use operators with side effects *improperly* inside an expression (I'm not sure what you mean by "equation"). For example, "x = y++;" is perfectly legitimate. Even if you choose not to write code like that, you'll need to be able to read and understand it. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Mar 12 '07 #30

 P: n/a Adrian Hawryluk wrote: x = y++ + y++; That has no sequence point between the y++s. x could equal to (y+1)+(y+1) or (y+1)+(y+2). This goes for all operators except comma ',' which is defined as a sequence point. There are also other operators besides the comma operator which are sequence points. -- pete Mar 12 '07 #31

 P: n/a

 P: n/a On Mar 12, 10:34 am, Adrian Hawryluk

 P: n/a "su**************@yahoo.com, India" int main(void) { void *p; printf("%p", p+1); return 0; } --------- Mar 12 '07 #34

 P: n/a we******@gmail.com writes: I saw this question fromwww.brainbench.comvoid *ptr;myStruct myArray[10];ptr = myArray;Which of the following is the correct way to increment the variable"ptr"? [snip] >Choice 4 ptr = ptr + sizeof(myArray); This is a possible answer. sizeof (myArray) referrs to the whole array at once (essential 10 sequential myStruct's). So if the unit of increment that the question is referring to is the whole myArray, then this increments to the position right past it. Technically this is legal in C. No, it isn't. C does not define arithmetic on void* pointers (though some compilers may do so as an extension). [...] >Choice 5 ptr = ptr + sizeof(myStruct); This also a possible correct answer (and the more likely one.) Again, C does not define arithmetic on void* pointers. [...] All five solutions, with the possible exception of "increment(ptr);" (*if* increment is defined appropriately as a macro) are illegal. More precisely, they're constraint violations, requiring compiler diagnostics from any conforming C compiler. This has already been discussed at considerable length. If you missed the discussion, you can see it at groups.google.com. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Mar 13 '07 #35

 P: n/a Miojo Lamen int main(void) { void *p; printf("%p", p+1); return 0; } --------- With "gcc -ansi -pedantic", or even just "gcc -pedantic", I get: c.c: In function `main': c.c:7: warning: pointer of type `void *' used in arithmetic That's gcc 3.4.4 (not "DevC++"). -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Mar 13 '07 #36

 P: n/a su**************@yahoo.com, India said: I saw this question from www.brainbench.com void *ptr; myStruct myArray[10]; ptr = myArray; [...] > THEIR Correct Answer given in this site is: ptr = ptr + sizeof(myStruct); If that is the case, then the Brainbench test remains meaningless. But my answer is ++(int*)ptr. That's wrong, too. 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 ? Avoiding the Brainbench test would be favourite, I think. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Mar 13 '07 #37

 P: n/a Adrian Hawryluk wrote: > .... snip ... > Still leads to bad code. Take for instance: x = y++ + y++; That has no sequence point between the y++s. x could equal to (y+1)+(y+1) or (y+1)+(y+2). This goes for all operators except comma ',' which is defined as a sequence point. Or anything else, since the result is undefined. Some models of the DS9000 will exterminate the programmer. -- If you want to post a followup via groups.google.com, ensure you quote enough for the article to make sense. Google is only an interface to Usenet; it's not Usenet itself. Don't assume your readers can, or ever will, see any previous articles. More details at: -- Posted via a free Usenet account from http://www.teranews.com Mar 13 '07 #38

 P: n/a J. J. Farrell wrote: On Mar 12, 10:34 am, Adrian Hawryluk Malcolm McLean wrote: >>"Adrian Hawryluk"

 P: n/a pete wrote: Adrian Hawryluk wrote: >x = y++ + y++;That has no sequence point between the y++s. x could equal to(y+1)+(y+1) or (y+1)+(y+2). This goes for all operators except comma',' which is defined as a sequence point. There are also other operators besides the comma operator which are sequence points. Yes, but I didn't describe any here except for the '='. Adrian -- ================================================== ======== Adrian Hawryluk BSc. Computer Science ---------------------------------------------------------- Specialising in: OOD Methodologies in UML OOP Methodologies in C, C++ and more RT Embedded Programming __--------------------------------------------------__ ----- [blog: http://adrians-musings.blogspot.com/] ----- '--------------------------------------------------------' My newsgroup writings are licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License http://creativecommons.org/licenses/by-nc-sa/3.0/ ================================================== ======== Mar 13 '07 #40

 P: n/a Keith Thompson wrote: Adrian Hawryluk Malcolm McLean wrote: >>"Adrian Hawryluk"

 P: n/a Adrian Hawryluk wrote: > pete wrote: Adrian Hawryluk wrote: x = y++ + y++; That has no sequence point between the y++s. x could equal to (y+1)+(y+1) or (y+1)+(y+2). This goes for all operators except comma ',' which is defined as a sequence point. There are also other operators besides the comma operator which are sequence points. Yes, but I didn't describe any here except for the '='. I don't understand your meaning. The assignment operator is not a sequence point. The comma operator has nothing to do with the posted code, so there is no reason to treat the comma operator differently from any other operators which have nothing to do with the posted code. -- pete Mar 13 '07 #42

 P: n/a Adrian Hawryluk wrote: > pete wrote: Adrian Hawryluk wrote: pete wrote:Adrian Hawryluk wrote:x = y++ + y++;That has no sequence point between the y++s. x could equal to(y+1)+(y+1) or (y+1)+(y+2).This goes for all operators except comma',' which is defined as a sequence point.There are also other operators besides the comma operatorwhich are sequence points. Yes, but I didn't describe any here except for the '='. I don't understand your meaning. The assignment operator is not a sequence point. Er, When evaluating an initializer. Not the '=' exactly. I still don't see what you're talking about. There is no initializer being evaluated in the posted code. The expression (i = ++i + 1) is given as an example of undefined behavior in the c99 standard. If assignment was a sequence point, there would be nothing wrong with that expression, but assignment just simply isn't a sequence point. The comma operator has nothing to do with the posted code, x = y++, y++; Happy? This is a defined expression with a defined result. (x = y++ && y++) also a defined expression. I don't understand why you said that the comma operator is unique as an operator with a sequence point. so there is no reason to treat the comma operator differently from any other operators Actually, there is reason as some are defined as sequence points and others are not. which have nothing to do with the posted code. I had assumed that you could follow the progression. My mistake. There was no progression to follow. -- pete Mar 14 '07 #44

 P: n/a pete wrote: > .... snip ... > (x = y++ && y++) also a defined expression. No it isn't. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. -- Posted via a free Usenet account from http://www.teranews.com Mar 14 '07 #46

 P: n/a CBFalconer wrote: pete wrote: >> ... snip ... >>(x = y++ && y++) also a defined expression. No it isn't. Is it not? The && introduces a sequence point. Am I missing something? -- Chris "electric hedgehog" Dollin "We live for the One, you die for the One." Unsaid /Babylon 5/. Mar 14 '07 #47

 P: n/a In article <45***************@yahoo.com>, CBFalconer (x = y++ && y++) also a defined expression. >No it isn't. Whyever not? -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963. Mar 14 '07 #48

 P: n/a Chris Dollin wrote: CBFalconer wrote: pete wrote: > ... snip ... > (x = y++ && y++) also a defined expression. No it isn't. Is it not? The && introduces a sequence point. Am I missing something? #include int main(void) { int x = 0, y = 0; (x = y++ && y++); printf("x == %d\ty == %d\n", x, y); return 0; } \$ ./0102 x == 0 y == 1 \$ Mar 14 '07 #49

 P: n/a santosh wrote: Chris Dollin wrote: >CBFalconer wrote: pete wrote: ... snip ...(x = y++ && y++) also a defined expression. No it isn't. Is it not? The && introduces a sequence point. Am I missingsomething? #include int main(void) { int x = 0, y = 0; (x = y++ && y++); printf("x == %d\ty == %d\n", x, y); return 0; } \$ ./0102 x == 0 y == 1 \$ Um, you missed out the explanation. What are you claiming to demonstrate? -- Chris "electric hedgehog" Dollin "Answer the unanswered riddle." /The Riddle-Master of Hed/ Mar 14 '07 #50

53 Replies

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more C / C++ Questions on Bytes