C vs Java array

A

ankur.pathela

Hi All,

I tried the following code in java:

/************************/
int i=0;
int[] arr={10,20,30};
arr=i=1;
/************************/

It changed arr to {1,20,30}

I aquaint to have written a similar code in C long time back and the
array was then changed to {10,1,30} (Please correct me if I am wrong).

Even in java, associativity is from right to left in case of '='
operator. Then why is this happening?

Regards,
Ankur
 
S

SPG

Hi All,

I tried the following code in java:

/************************/
int i=0;
int[] arr={10,20,30};
arr=i=1;
/************************/

It changed arr to {1,20,30}

I aquaint to have written a similar code in C long time back and the
array was then changed to {10,1,30} (Please correct me if I am wrong).

Even in java, associativity is from right to left in case of '='
operator. Then why is this happening?

Regards,
Ankur



Looks to me like the compiler is reading this as:

arr = i;
i = 1;

seems fair enough to me..
 
S

SPG

SPG said:
Hi All,

I tried the following code in java:

/************************/
int i=0;
int[] arr={10,20,30};
arr=i=1;
/************************/

It changed arr to {1,20,30}

I aquaint to have written a similar code in C long time back and the
array was then changed to {10,1,30} (Please correct me if I am wrong).

Even in java, associativity is from right to left in case of '='
operator. Then why is this happening?

Regards,
Ankur



Looks to me like the compiler is reading this as:

arr = i;
i = 1;

seems fair enough to me..


I'll amend my own post!
and read:

arr = (i=1);

Therefore the left side will still be arr element 0 as i is still 0, then
assign the new value to i on the right side and assign the new value..

Still seems fair enough!
 
T

Thomas Hawtin

/************************/
int i=0;
int[] arr={10,20,30};
arr=i=1;
/************************/

It changed arr to {1,20,30}

I aquaint to have written a similar code in C long time back and the
array was then changed to {10,1,30} (Please correct me if I am wrong).


The vast majority of compilers will do that. However, IIRC, the order of
execution in this case is undefined in C. The result is a side effect of
the parsing technique, and apparently to make pushing arguments of
functions onto the stack easier.
Even in java, associativity is from right to left in case of '='
operator. Then why is this happening?

The associativity of = is the same. In both languages the statement
expression is parsed in the same way, i.e. arr = (i = 1);

Java evaluates in a well defined order of least surprise. The array
index expression comes before the assignment subexpression, so evaluated
first.

In general, even without the undefined nature in C, this sort of
expression, where a side-effect alters the interpretation of other parts
of the same statement, should be avoided like the plague.

Tom Hawtin
 
J

Jakob Bieling

Hi All,

I tried the following code in java:

/************************/
int i=0;
int[] arr={10,20,30};
arr=i=1;
/************************/

I aquaint to have written a similar code in C long time back and the
array was then changed to {10,1,30} (Please correct me if I am wrong).

I hope you did not write much in C, since, as far as I know, this is
undefined behaviour (someone correct me, if I am wrong); you are reading
and writing the same variable without a sequence point in between.

regards
 
K

Kenneth P. Turvey

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I hope you did not write much in C, since, as far as I know, this
is undefined behaviour (someone correct me, if I am wrong); you are
reading and writing the same variable without a sequence point in
between.

I'm an old C programmer. This kind of question comes up all the time
with C. Many programmers are obsessed with understanding exactly what
their platform does in some strange situation. I have a couple rules of
thumb about such things:

1) You are better off not knowing.
2) If you find code that you don't understand due to things like this,
it is a bug.

That is to say you should never write code that depends on the details
of parsing ambiguous expressions. If you can't immediately tell what
the code is going to do, you shouldn't write it that way.

The real answer to this question is:

Fix the line, don't ask what it means.


- --
Kenneth P. Turvey <[email protected]>
http://kt.squeakydolphin.com (not much there yet)
Jabber IM: (e-mail address removed)
Phone: (314) 255-2199
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDMWrx3naBnF2rJNURAkAzAJ99afZNY5wFpobHhjsMcLiIUQ42+QCeIDmY
7jFvlVepj6ps4iJk7lE7fwI=
=+PFW
-----END PGP SIGNATURE-----
 
T

Thomas Hawtin

Kenneth said:
Fix the line, don't ask what it means.

It probably helps to understand what it is doing in order to fix it. If
the docs disagree with behaviour that's already out there, best fix the
docs.

Tom Hawtin
 
K

Kenneth P. Turvey

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It probably helps to understand what it is doing in order to fix it.
If the docs disagree with behaviour that's already out there, best fix
the docs.

This line does not adequately describe what the programmer intended the
program to do. Fixing it isn't difficult, simply breaking it into a
series of statements that do what you actually want done solves the
problem and makes the code more readable.

The docs should be correct. That said, this is the kind of thing that
should never be written in a program so no matter what the docs say,
this is a bug.

- --
Kenneth P. Turvey <[email protected]>
http://kt.squeakydolphin.com (not much there yet)
Jabber IM: (e-mail address removed)
Phone: (314) 255-2199
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDMXgN3naBnF2rJNURApJoAJ4pAloERF5R3PJ96IwDcolXLWe6QQCeJeES
dbdwtgp6myI1R8HRpOLPGZ4=
=47kH
-----END PGP SIGNATURE-----
 
A

Ankur

Thank you all for ur replies.
JB, I used to write in C earlier but I never tried different compilers.

Thnx again, for ur help

regards,
ankur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top