simple question about a pointer to struct initialization

Discussion in 'C Programming' started by mcheema, Feb 15, 2014.

  1. mcheema

    mcheema Guest

    struct mystruct *my=malloc(sizeof *my);
    Is the reason this works: that the line is actually two statements the first creates the pointer my and the second initializes it?
    mcheema, Feb 15, 2014
    1. Advertisements

  2. mcheema

    Eric Sosman Guest

    Technically the line is not a "statement" but a "declaration."
    A "statement" is something like `while' or `return' or `x = 42'.
    A "declaration" introduces and describes an identifier.

    Your declaration can be read in two parts. First comes the
    `struct mystruct *my' piece, which introduces the identifier `my'
    as a variable and describes its type. The `=malloc(sizeof *my)'
    piece is an "initializer" that assigns the new variable a starting
    value. The `sizeof *my' piece is well-defined because the type
    of `my' has already been established by the first part of the
    declaration, before the initializer needs to be evaluated.
    Eric Sosman, Feb 15, 2014
    1. Advertisements

  3. Technically, it's zero statements, because C distinguishes between
    declarations (like this) and statements (like an assignment expression
    on its own).

    It certainly has two parts: the bit before the = could stand on its own
    as a declaration (with a ; of course), and the part after the = (an
    initialiser) gives the initial value for the pointer object being
    declared. The value could be provided by writing a separate assignment.
    Ben Bacarisse, Feb 15, 2014
  4. mcheema

    mcheema Guest

    Thanks Eric and Ben for the responses. So firstly it is a declaration not a statement and secondly the type of my is defined at the point sizeof is applied to *my.
    mcheema, Feb 15, 2014
  5. mcheema

    Eric Sosman Guest


    For completeness, there are a few situations where `sizeof' could
    not be evaluated. One is when `struct mystruct' is an "incomplete"
    type, that is, when there has been no

    struct mystruct {
    double trouble;
    short shrift;
    char broiled;

    declaration earlier giving the details of the `struct mystruct' type.
    (Your example didn't show such a prior declaration, but it would need
    to have been there for your example to work at all, so both Ben and I
    "took it as read.") You can form pointers to objects of incomplete
    type, but the size (and details) of the pointed-to object is unknown.

    Another is when declaring an array of unspecified size, where
    the initializers determine the array size. For example,

    int array[] = { 1, 2, 3, 4 }; // four elements
    char string[] = "Hello, world!"; // fourteen (13+1) elements

    are perfectly all right. But in these cases, the initializers
    govern the array sizes so `sizeof array' and `sizeof string' aren't
    known until after the initialization. Something like

    int wrong[] = { 1, 2, 3, sizeof wrong, 42 };

    would not work.
    Eric Sosman, Feb 15, 2014
  6. And because the operand of sizeof is not evaluated, but merely analyzed
    to determine its type (unless it's a variable-length array, which
    doesn't apply here).
    Keith Thompson, Feb 16, 2014
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.