expression-statement question

Discussion in 'C Programming' started by Chad, Jun 11, 2010.

  1. Chad

    Chad Guest

    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
     
    Chad, Jun 11, 2010
    #1
    1. Advertising

  2. Chad

    Ben Pfaff Guest

    Chad <> writes:

    > 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
     
    Ben Pfaff, Jun 11, 2010
    #2
    1. Advertising

  3. Chad <> writes:
    > 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 11, 2010
    #3
  4. Chad

    Chad Guest

    On Jun 10, 9:52 pm, Keith Thompson <> wrote:
    > Chad <> writes:
    > > 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.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?'.
     
    Chad, Jun 11, 2010
    #4
  5. Chad

    Seebs Guest

    On 2010-06-11, Chad <> 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 /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jun 11, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    835
  2. Jay McGavren
    Replies:
    11
    Views:
    1,184
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,164
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    495
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,088
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page