473,387 Members | 1,863 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.

explain the code snippet

int main()
{
struct num {
int x,y;
} val[4] = {1,1,2,3,4,5,6,7};
struct num *ptr = val;
int i=0;
for(;i<4;i++) {
ptr->x = ptr->y, ++ptr++->y;
printf("%d,%d ", val[i].x, val[i].y);
}
}

What does "," do in ptr->x = ptr->y, ++ptr++->y; ?

AK

May 11 '07 #1
10 1462
Ajinkya wrote On 05/11/07 13:34,:
int main()
{
struct num {
int x,y;
} val[4] = {1,1,2,3,4,5,6,7};
struct num *ptr = val;
int i=0;
for(;i<4;i++) {
ptr->x = ptr->y, ++ptr++->y;
printf("%d,%d ", val[i].x, val[i].y);
}
}

What does "," do in ptr->x = ptr->y, ++ptr++->y; ?
It confuses people. Look up "comma operator" in
your C textbook or reference.

--
Er*********@sun.com
May 11 '07 #2
On May 11, 11:20 am, Eric Sosman <Eric.Sos...@sun.comwrote:
Ajinkya wrote On 05/11/07 13:34,:
int main()
{
struct num {
int x,y;
} val[4] = {1,1,2,3,4,5,6,7};
struct num *ptr = val;
int i=0;
for(;i<4;i++) {
ptr->x = ptr->y, ++ptr++->y;
printf("%d,%d ", val[i].x, val[i].y);
}
}
What does "," do in ptr->x = ptr->y, ++ptr++->y; ?

It confuses people. Look up "comma operator" in
your C textbook or reference.
Sorry sir, very trivial question. I got the answer before anyone
posted so sorry to disturb you all.
>
--
Eric.Sos...@sun.com

May 11 '07 #3
On Fri, 11 May 2007 10:34:29 -0700, Ajinkya wrote:
int main()
{
struct num {
int x,y;
} val[4] = {1,1,2,3,4,5,6,7};
struct num *ptr = val;
int i=0;
for(;i<4;i++) {
ptr->x = ptr->y, ++ptr++->y;
printf("%d,%d ", val[i].x, val[i].y);
}
}

What does "," do in ptr->x = ptr->y, ++ptr++->y; ?

AK
I guess this program is demonstrating operator priorities.
It shows
1.-have higher priority than ++, so
++ptr++->y
means:
++(ptr->y);
ptr++;
2. , have the lowest priority. So the effect of
ptr->x = ptr->y,++ptr++->y;
equals to
ptr->x = ptr->y;
++ptr++->y;
Though internally, they are different.
May 11 '07 #4
Ajinkya said:
int main()
{
struct num {
int x,y;
} val[4] = {1,1,2,3,4,5,6,7};
struct num *ptr = val;
int i=0;
for(;i<4;i++) {
ptr->x = ptr->y, ++ptr++->y;
printf("%d,%d ", val[i].x, val[i].y);
}
}

What does "," do in ptr->x = ptr->y, ++ptr++->y; ?
See K&R2, page 264, in between "coercion" and "command-line arguments".

Note that the behaviour of the program is undefined.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 11 '07 #5
Ajinkya wrote:
>
int main()
{
struct num {
int x,y;
} val[4] = {1,1,2,3,4,5,6,7};
struct num *ptr = val;
int i=0;
for(;i<4;i++) {
ptr->x = ptr->y, ++ptr++->y;
printf("%d,%d ", val[i].x, val[i].y);
}
}

What does "," do in ptr->x = ptr->y, ++ptr++->y; ?
It discards the previous expression (ptr->y) and evaluates the next
(suspicious) one, (++ptr++->y).

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

May 12 '07 #6
>Ajinkya wrote:
[given suitable items, which I snipped, and]
> ptr->x = ptr->y, ++ptr++->y;
What does "," do in ptr->x = ptr->y, ++ptr++->y; ?
In article <46***************@yahoo.com>
CBFalconer <cb********@maineline.netwrote:
>It discards the previous expression (ptr->y) ...
Actually, since the binding of "," is done very late, the previous
expression is "ptr->x = ptr->y".
>and evaluates the next (suspicious) one, (++ptr++->y).
Right.

This one binds as ++((ptr++)->y), from which it should be clear
how to interpret it:

Remember that "a++", for some object a, fetches the value stored
in a, adds 1 to that, schedules the new value to be put back into
the object by the next sequence point, and produces (as the value
of the postfix-++ operator) the old value of the object. So
"ptr++" means "the value that was in ptr before it gets
incremented, although the increment may happen right away, or
not, at the whim of the compiler."

In "expr->member", the expression on the left is evaluated. It
must have type "pointer to struct" (or "pointer to union"), and
the structure (or union) in question must have the named member.
The result of such an expression is named member-object.

Finally "++b", for some object b, is much like "a++", except
that the value produced by the prefix-++ operator is the new
value that will be stored into b. This value is produced whether
or not b is actually incremented at that point; all you get is
the promise that the increment will occur by the next sequence
point.

The whole thing is generally bad form since a reader must sit down
and puzzle out the individual object-modifications, making sure
that no two are applied to the same object without an intervening
sequence point. It is much clearer if one writes:

ptr->x = ptr->y;
ptr->y++;
ptr++;

although I would not be unhappy with the shorter:

ptr->x = ptr->y++;
ptr++;

myself. (This squeezes two separate side effects -- modifying both
ptr->x and ptr->y -- into a single statement, but it seems clear
that ptr->x and ptr->y are distinct objects. Of course, "seeming
clear" and "being the case" are not always identical: perhaps
earlier someone did "#define x y", or "ptr" points to a union
containing "int x; double y;". However, the union trick would give
undefined behavior even with the three-statement version.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
May 12 '07 #7

"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:xd******************************@bt.com...
Ajinkya said:
>int main()
{
struct num {
int x,y;
} val[4] = {1,1,2,3,4,5,6,7};
struct num *ptr = val;
int i=0;
for(;i<4;i++) {
ptr->x = ptr->y, ++ptr++->y;
printf("%d,%d ", val[i].x, val[i].y);
}
}

What does "," do in ptr->x = ptr->y, ++ptr++->y; ?

See K&R2, page 264, in between "coercion" and "command-line arguments".

Note that the behaviour of the program is undefined.
Why? ++ptr++->y means ++((ptr++)->y) which is perfectly defined.
printf returns an int, so at least in C90 omitting its declaration
is allowed (though I can't see any reason to do that). And at the
end prt points to val[4], which is allowed as long as that pointer
isn't dereferenced. The problem is that main doesn't return, and
that output doesn't end with \n?
In C99, if we #include <stdio.hand add putchar('\n'); at the end of the
block of main this program is legal, however ugly it is.
In C90 if we add putchar('\n'); return 0; at the end the program is
legal, isn't it?
(Correct me if I'm wrong.)
May 12 '07 #8
Army1987 said:
>
"Richard Heathfield" <rj*@see.sig.invalidha scritto nel messaggio
news:xd******************************@bt.com...
<snip>
>Note that the behaviour of the program is undefined.

Why? ++ptr++->y means ++((ptr++)->y) which is perfectly defined.
I was not referring to that statement. I was referring to the behaviour
of the program, which is indeed undefined. This is because the program
calls a variadic function without a valid prototype in scope.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 12 '07 #9
Chris Torek wrote:
CBFalconer <cb********@maineline.netwrote:
>It discards the previous expression (ptr->y) ...

Actually, since the binding of "," is done very late, the previous
expression is "ptr->x = ptr->y".
>and evaluates the next (suspicious) one, (++ptr++->y).

Right.

This one binds as ++((ptr++)->y), from which it should be clear
how to interpret it:
I had no desire to check it out, so I simply labelled it
'suspicious'.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

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

May 12 '07 #10
Eric Sosman wrote:
Ajinkya wrote On 05/11/07 13:34,:
>int main()
{
struct num {
int x,y;
} val[4] = {1,1,2,3,4,5,6,7};
struct num *ptr = val;
int i=0;
for(;i<4;i++) {
ptr->x = ptr->y, ++ptr++->y;
printf("%d,%d ", val[i].x, val[i].y);
}
}

What does "," do in ptr->x = ptr->y, ++ptr++->y; ?

It confuses people.
/Some/ people ... It's just like ";", but inside an expression.

It's the `++ptr++->y` that has the potential for confusion, I'd've
thought. I'd unpack it somewhat:

ptr->x = ptr->y, ptr->y += 1, ptr += 1;

Although (like Chris Torek elsethread, I see) I'd not be unhappy
to collapse the first two together:

ptr->x = ptr->y++, ptr += 1;

Note that my personal religion doesn't use `++` when the result
value is irrelevant, hence `ptr += 1` rather than `ptr++`, but
if you'd prefer the `++` then that's OK too.
Look up "comma operator" in your C textbook or reference.
Indeed. (And note that the comma operator is /not/ what separates
arguments to functions.)

--
Scoring, bah. If I want scoring I'll go play /Age of Steam/.

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

May 14 '07 #11

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

Similar topics

11
by: Kalle Rutanen | last post by:
Hello Here is a short code snippet which does not compile. Could someone explain why this is ? class A { public: void set(int a) {
4
by: Rob Conner | last post by:
No you don't need to know Zope to help me. The whole reason I'd even want to do this is because of Zope though. I made a Zope product, and now want to perfect it. some simple example code... ...
4
by: Tweaxor | last post by:
Hey, forgive me of my ignorance! Can someone tell me why this won't work. I have this coded but it won't compile is this not permitted in C. kg = kilograms and d = distance // this is not...
6
by: amerar | last post by:
Hi All, I'm not good at Javascript, so I am trying to understand this small bit of code: var groups=document.$fm.category.options.length; var group=new Array(groups); for (i=0; i<groups;...
24
by: muttaa | last post by:
i've a code snippet below: for(;0;) printf("Gud Morning"); when this is run,i get the "gud morning" message printed,eventhough the
2
by: daokfella | last post by:
Hi all, I've created a code snippet in VS 2005 and saved it in the proper snippet folder. When I right-click and select Insert Snippet, I navigation to My Code Snippets and it shows my snippet....
3
by: Hardy | last post by:
I created a code snippet which is for displaying file name,Line number and function name in C#.NET project. // <File> sf.GetFileName() // </File> // <Line> sf.GetFileLineNumber().ToString()...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
1
by: webinfinite | last post by:
HI, I ran across a code written by somebody else. Since I am new to C++, could you please explain what each of "const *" means in this code snippet as well as the last "const"? Tool const*...
4
by: tshad | last post by:
I was watching a video that used a code snippet to create a property and when you type "prop" tab tab, it would create the private variable as well as the property definitions with the private...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.