473,387 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4712
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******@hotmail.comwrote in message
news:55**************@mid.individual.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.
(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******@hotmail.comwrote in message
news:55**************@mid.individual.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******@hotmail.comwrote in message
news:55**************@mid.individual.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.
(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_Keith) 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*********@gmail.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_Keith) 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

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>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 "++".
it doesnt work in C++ either. +1 would be best

Mar 9 '07 #11
In article <vd******************************@giganews.com>,
P.J. Plauger <pj*@dinkumware.comwrote:
>>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.
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 interpretation
is 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
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>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.)
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
Keith Thompson wrote:
"santosh" <sa*********@gmail.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.
Yes, careless of me. It's unlikely to be a macro though, given that
it's in lower case.

Mar 9 '07 #14
On Mar 9, 11:02 am, "santosh" <santosh....@gmail.comwrote:
Keith Thompson wrote:
"santosh" <santosh....@gmail.comwrites:
subramanian10...@yahoo.com, India wrote:
>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
<stdio.his #included) is one I can think of off the top of my head,
and I know there are others. Anyway, the problem here is that the
answer to 'what is the correct way to increment a void* variable' is
'you don't increment void* variables'; incrementing a void* variable
can't sensibly be defined because void is an incomplete type. The
(incorrect, because sizeof(void) can't be taken) "ptr = (char*)ptr +
sizeof(void);" would come nearest to what the question is asking. On
the other hand, the answer to "what is the correct way to cause ptr to
point at the element of myArray after the one it's currently pointing
at" would be ptr=1+(myStruct*)ptr, assuming that myStruct is a typedef
for a user-defined struct type (which would seem plausible from the
name, but this hasn't been stated). Out of the choices actually given,
2 is the only one that stands a chance of being correct (if increment
is a carefully-defined macro); 1, 4, and 5 all try to do arithmetic on
void* (impossible because void is incomplete), and 3 isn't legal in C
(but is an extension I've seen on more than one compiler, and which I
would use in some cases if it were portable, obviously using myStruct*
rather than int*).
--
ais523

Mar 9 '07 #15
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
On Mar 9, 12:43 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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);

THEIR Correct Answer given in this site is: ptr = ptr +
sizeof(myStruct);
Who is 'THEIR'?
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 ?
I would say "none of the above... maybe".
Choice 1 ptr = ptr + sizeof(ptr);
No, ptr is a void * and void has no size, thus it cannot move the
pointer and is considered an error.
Choice 2 increment(ptr);
This cannot work in C, there is no way of passing ptr by reference
unless you do so explicitly with the & operator.
Choice 3 ++(int*)ptr;
This is the maybe as this may or may not produce an lvalue given
certain compilers. It is not recommended.
Choice 4 ptr = ptr + sizeof(myArray);
Same answer as Choice 1.
Choice 5 ptr = ptr + sizeof(myStruct);
Same answer as Choice 1.

That said, choice 2 and 3 are close, and could work if tweaked:

Choice 2
increment(ptr); // wrong, passing by value cannot change
ptr's value
increment(&ptr); // could work if increment takes a ** and
wraps what actually happens.
Choice 3
++(int*)ptr;
++*(int**)&ptr;

That said, my mod to choice 3 may not work either under some systems
as I think that I read somewhere that conversion of a pointer may not
work as there are special pointers to certain types. I.e. a int
pointer may not be the same as a double pointer. Unfortunately, I
don't remember the reference but should still work in most cases.

The safest bet is doing this:

int * intPtr = (int*)ptr;
++intPtr;
ptr = (void*)intPtr;

Which in my mind would be what is happening internally if you did +
+*(int**)&ptr; If increment is wrapped in a macro which does this,
than it would work. A more generalised macro would be:

#define increment(fromType, toType, ptr) do { \
toType * _tmpPtr_ = (toType*)ptr; \
++_tmpPtr_; \
ptr = (fromType*)_tmpPtr_; \
} while (0)

and then do this:

increment(void, int, ptr);

An increment function that assumes to increment a void pointer as if
it were an int would be:
void increment(void** prt)
{
int* intPtr = (int*)*prt;
++intPtr;
*ptr = (void*)intPtr;
}

but a more generalised solution would be this:

void increment(void** ptr, size_t size)
{
char* charPtr = (char*)*prt;
charPtr += size;
*ptr = (void*)charPtr;
}

the first would be used like:

increment(&ptr);

the second would be used like:

increment(&ptr, sizeof(int));

Does this make sense to you?
Adrian

Mar 9 '07 #17
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 = <whatever>; // assigned to void *
++p.ip;
Adrian
Mar 9 '07 #18
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 = <whatever>; // 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
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
spacecriter (Bill C) wrote:
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++];
What about it?

--
Adrian
Mar 9 '07 #21
Adrian Hawryluk wrote:
spacecriter (Bill C) wrote:
>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++];
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
Richard Tobin wrote:
In article <vd******************************@giganews.com>,
P.J. Plauger <pj*@dinkumware.comwrote:
>>>>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.

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 interpretation
is 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
spacecriter (Bill C) wrote:
Adrian Hawryluk wrote:
>spacecriter (Bill C) wrote:
>>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++];
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
"Ian Collins" <ia******@hotmail.comwrote in message
news:55**************@mid.individual.net...
Richard Tobin wrote:
>In article <vd******************************@giganews.com>,
P.J. Plauger <pj*@dinkumware.comwrote:
>>>>>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.

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 interpretation
is 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.
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

"Adrian Hawryluk" <ad**************************@nospam.comwrote
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.
Preincrement can go.
Postincrement is idiomatic. It means "do something to this, then go on to
the next one".
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 11 '07 #26
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
On Mar 9, 6:51 pm, "P.J. Plauger" <p...@dinkumware.comwrote:
>
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
Malcolm McLean wrote:
>
"Adrian Hawryluk" <ad**************************@nospam.comwrote
>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.
Preincrement can go.
Postincrement is idiomatic. It means "do something to this, then go on
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.
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 12 '07 #29
Adrian Hawryluk <ad**************************@nospam.comwrites:
Malcolm McLean wrote:
>"Adrian Hawryluk" <ad**************************@nospam.comwrote
>>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.
Preincrement can go.
Postincrement is idiomatic. It means "do something to this, then go
on 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 <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 12 '07 #30
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
<subramanian10...@yahoo.comwrote:
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"?
Increment it with respect to what? Its a void * pointer. If they
mean increment it with respect to the unit of item its pointing at
(likely) then they want you to treat ptr as if it were a myStruct *
rather than a void * pointer. But it is also pointing at a
myStruct[10] array (excuse the Java syntax, C doesn't allow for
ordinary english discourse on this matter) so the question is not well
defined.
Choice 1 ptr = ptr + sizeof(ptr);
There is no indication that ptr is pointing at an array of items that
is the same time as the ptr itself (I think this is a logical
impossiblity; though I could be proven wrong -- I am too lazy to work
it out either way at the moment.) Intuitively, in this case, void *
does not point to void * (that would be void **). If you can
establish that sizeof(void*) == sizeof(myStruct) or sizeof(myArray),
then its hard to make a case for the correctness of this answer.
Choice 2 increment(ptr);
This is not Pascal. But technically I guess it would depend on
whether or not a macro named "increment()" was defined that made this
have a valid interpretation that "incremented ptr".
Choice 3 ++(int*)ptr;
There is no definitively described int here. If myStruct has been
defined at the preprocessor level or via a typedef to be an int, then
this is technically ok, but even then would be in bad form.
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.

If ptr is meant to be pointing at the entries *in* the myArray array
then, this would not be correct. But because the thing is a void *,
we really don't have a clue as to what the programmer really means
here.
Choice 5 ptr = ptr + sizeof(myStruct);
This also a possible correct answer (and the more likely one.) Here
we are treating ptr as if it were a pointer to the *insides* of
myStruct. I.e., as if ptr is really a myStruct * pointer. The result
after this operation will be to make ptr equal to &myStruct[1].

Now the problem with this, of course, is that there is no good reason
to declare ptr as a void *, if this is how you intend to use it. So
this is a question about the output/result of code that is written in
very bad form.
THEIR Correct Answer given in this site is: ptr = ptr +
sizeof(myStruct);
Of course. They didn't think to hard when making or answering the
question.
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.
Yeah, but the question is trying to imply that there is an
*appropriate* object, I think. I can see how you are technically
correct here as well, since you basically just make ptr into something
that can be incremented, then increment it.

However since myStruct is not described, it might be typdefed to a
char or something, and your platform might, theoretically have
sizeof(int)=16 (this will occurr or rare machines in roughly 190 years
from now, if you believe in Moore's Law continuing) at which point ptr
will be pointing *past* the end of any knowably legally defined C
object, which would make this wrong.
Which is correct ?
Well they told you which is the correct answer, so I guess that's the
"correct" one. The question isn't so much about legal C, I think, as
it is about "Can you think like the kind of drone we want you to be?"
I mean the questioner has some notions about how one might interpret a
void * pointer in this context, and he just wants you to interpret it
the same way. Nevermind that there isn't a real iron clad way or
reason for doing so (C standard or not).

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Mar 12 '07 #32
On Mar 12, 10:34 am, Adrian Hawryluk <adrian.hawryluk-at-
gmail....@nospam.comwrote:
Malcolm McLean wrote:
"Adrian Hawryluk" <adrian.hawryluk-at-gmail....@nospam.comwrote
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.
Preincrement can go.
Postincrement is idiomatic. It means "do something to this, then go on
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.
Why is that best? Is it best to not use cars because car drivers
sometimes hit things or people? I'd say the best thing to do in C is
to use C operators correctly and write correct C.

Mar 12 '07 #33
"su**************@yahoo.com, India" <su**************@yahoo.comwrote in
news:11*********************@v33g2000cwv.googlegro ups.com:
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 ?
I have also (wrongly) assumed once that void* had the same arithmetic of a
char*.

So, the answers ALL wrong. The resumee of all answers given was:

1,4 and 5 due to the fact that void* have no arithmetic.
(Choice 5 would be correct if void* has the arithmetic of a char*. Read PS
below)
2 because it would be a macro. But no type is passed.
3 would be correct if myStruct had the same size of an int. But it has the
problem that a casted variable is not a lvalue. (Even in this situation Gcc
compiles and only issues a message of a deprecated feature)

Isn't it?

Regards

PS. A little note
Some compilers (Gcc, under DevC++ 4.9.9.2) don't even issue any warnings to
the following code (which I have compiled in strictly ansi mode)
------------
#include <stdio.h>

int main(void)
{
void *p;

printf("%p", p+1);

return 0;
}
---------
Mar 12 '07 #34
we******@gmail.com writes:
<subramanian10...@yahoo.comwrote:
>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"?
[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 <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 13 '07 #35
Miojo Lamen <mi***@nissin.comwrites:
[...]
PS. A little note
Some compilers (Gcc, under DevC++ 4.9.9.2) don't even issue any warnings to
the following code (which I have compiled in strictly ansi mode)
------------
#include <stdio.h>

int main(void)
{
void *p;

printf("%p", p+1);

return 0;
}
---------
<OT>
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++").
</OT>

--
Keith Thompson (The_Other_Keith) 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 13 '07 #36
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
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: <http://cfaj.freeshell.org/google/>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 13 '07 #38
J. J. Farrell wrote:
On Mar 12, 10:34 am, Adrian Hawryluk <adrian.hawryluk-at-
gmail....@nospam.comwrote:
>Malcolm McLean wrote:
>>"Adrian Hawryluk" <adrian.hawryluk-at-gmail....@nospam.comwrote
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.
Preincrement can go.
Postincrement is idiomatic. It means "do something to this, then go on
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.

Why is that best? Is it best to not use cars because car drivers
sometimes hit things or people? I'd say the best thing to do in C is
to use C operators correctly and write correct C.
It can be if the car's wheels are apt to fall off, or the driver is
usually drunk. Yes, it is done, it can be done, it will be done. It is
just not always intuitive as to what is happening, and many people learn
a language by use and not reading a thick specification book of it.

I am not illegitimatising the use of it, I am saying that it makes the
code that much more difficult to read. When you have to do it, fine.
When you do it whimsically without need, then you should just enter the
C Obfuscation contest.

Clarity in programming makes for more maintainable, understandable code,
reducing costs across the board.
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 #39
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
Keith Thompson wrote:
Adrian Hawryluk <ad**************************@nospam.comwrites:
>Malcolm McLean wrote:
>>"Adrian Hawryluk" <ad**************************@nospam.comwrote
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.

Preincrement can go.
Postincrement is idiomatic. It means "do something to this, then go
on 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.
Agreed, somewhat. Read my further post on using when not necessary
making difficult to read code.
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 #41
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
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 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.
Er, When evaluating an initializer. Not the '=' exactly.
The comma operator
has nothing to do with the posted code,
x = y++, y++;

Happy? This is a defined expression with a defined result.
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.
Adrian
--
__________________________________________________ _________
/__________________Adrian_Hawryluk__BSc.___________ _________\
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/______
\________[blog: http://adrians-musings.blogspot.com/]_______/
Mar 14 '07 #43
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 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.

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
Adrian Hawryluk <ad**************************@nospam.comwrites:
J. J. Farrell wrote:
>On Mar 12, 10:34 am, Adrian Hawryluk <adrian.hawryluk-at-
gmail....@nospam.comwrote:
>>Malcolm McLean wrote:

"Adrian Hawryluk" <adrian.hawryluk-at-gmail....@nospam.comwrote
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.
Preincrement can go.
Postincrement is idiomatic. It means "do something to this, then go on
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.

Why is that best? Is it best to not use cars because car drivers
sometimes hit things or people? I'd say the best thing to do in C is
to use C operators correctly and write correct C.

It can be if the car's wheels are apt to fall off, or the driver is
usually drunk. Yes, it is done, it can be done, it will be done. It
is just not always intuitive as to what is happening, and many people
learn a language by use and not reading a thick specification book of
it.

I am not illegitimatising the use of it, I am saying that it makes the
code that much more difficult to read. When you have to do it,
fine. When you do it whimsically without need, then you should just
enter the C Obfuscation contest.

Clarity in programming makes for more maintainable, understandable
code, reducing costs across the board.
You're wasting your time here with that argument. Certain people here
are so convinced of their skills that they think it will never be
necessary to run a debugger over their code. No joking. They appear to
have almost zero experience of working on huge codebases with legacy
support spread over several continents.
Mar 14 '07 #45
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.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 14 '07 #46
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
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>(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
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 <stdio.h>

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
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 missing
something?

#include <stdio.h>

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

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

Similar topics

27
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...
4
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...
9
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.