Velocity Reviews > expression-statement question

# expression-statement question

Guest
Posts: n/a

 06-11-2010
Give the following...

#include <stdio.h>
#include <stdlib.h>

struct point{
int x;
int y;
};

int main(void)
{

struct point *points;

if ((points = malloc(sizeof(struct point))) == NULL) {
fprintf(stderr, "unable to allocate memory\n");
exit(1);
}

free(points);

exit(0);
}

Doesn't the if statement only expressions and not expression-
statements? Just curious, because (points = malloc(sizeof(struct
point)) is an expression-statement. How does this get converted to an
expression so that the if statement can use it?

Ben Pfaff
Guest
Posts: n/a

 06-11-2010

> if ((points = malloc(sizeof(struct point))) == NULL) {

[...]
> Doesn't the if statement only expressions and not expression-
> statements?

Yes.

> Just curious, because (points = malloc(sizeof(struct point)) is
> an expression-statement.

No, it's just an expression (if you add the missing ")" at the
end).
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms

Keith Thompson
Guest
Posts: n/a

 06-11-2010
> Give the following...

[snip]
> if ((points = malloc(sizeof(struct point))) == NULL) {

[...]
> }
>
> Doesn't the if statement only expressions and not expression-
> statements? Just curious, because (points = malloc(sizeof(struct
> point)) is an expression-statement. How does this get converted to an
> expression so that the if statement can use it?

(You're missing a verb in that first sentence, but I can from context.)

No, ``points = ...'' is not an expression statement, it's just an
expression. An assignment operator is just another operator (that
happens to have a side effect), and an assignment is an expression.
It yields the value of the target object after the assignment.

An expression *statement* is a statement consisting of an expression
followed by a semicolon. (The grammar also treats a null statement,
consisting of just a semicolon, as an expression statement; I'm
not sure why.)

It happens that assignment expressions most commonly appear in
expression statements:
x = 42;
but that's really just a special case.

(In some other languages, an assignment is a statement but not an
expression.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Guest
Posts: n/a

 06-11-2010
On Jun 10, 9:52*pm, Keith Thompson <(E-Mail Removed)> wrote:
> > Give the following...

> [snip]
> > * if ((points = malloc(sizeof(struct point))) == NULL) {

> [...]
> > * }

>
> > Doesn't the if statement only expressions and not expression-
> > statements? Just curious, because (points = malloc(sizeof(struct
> > point)) is an expression-statement. How does this get converted to an
> > expression so that the if statement can use it?

>
> (You're missing a verb in that first sentence, but I can from context.)
>
> No, ``points = ...'' is not an expression statement, it's just an
> expression. *An assignment operator is just another operator (that
> happens to have a side effect), and an assignment is an expression.
> It yields the value of the target object after the assignment.
>
> An expression *statement* is a statement consisting of an expression
> followed by a semicolon. *(The grammar also treats a null statement,
> consisting of just a semicolon, as an expression statement; I'm
> not sure why.)
>
> It happens that assignment expressions most commonly appear in
> expression statements:
> * * x = 42;
> but that's really just a special case.
>
> (In some other languages, an assignment is a statement but not an
> expression.)
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) *<http://www.ghoti.net/~kst>
> Nokia
> "We must do something. *This is something. *Therefore, we must do this."
> * * -- Antony Jay and Jonathan Lynn, "Yes Minister"

The original question was meant to be worded as 'Doesn't the if
statement only allow expressions and not expression-statements?'.

Seebs
Guest
Posts: n/a

 06-11-2010
On 2010-06-11, Chad <(E-Mail Removed)> wrote:
> The original question was meant to be worded as 'Doesn't the if
> statement only allow expressions and not expression-statements?'.

Okay. The answer is, yes, but you're confused. Assignment has nothing
to do with whether or not something is a statement. A semicolon does.

You can't write:
if (3
because the if statement takes an expression, not an expression-statement.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!