expression-statement question

C

Chad

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?

Chad
 
B

Ben Pfaff

Chad said:
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).
 
K

Keith Thompson

Chad said:
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.)
 
C

Chad

Chad said:
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 address 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?'.
 
S

Seebs

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
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top