Accessing members of a struct

Discussion in 'C Programming' started by joseph.paterson@gmail.com, Apr 11, 2006.

  1. Guest

    Hello,

    I'm having some trouble with accessing members of a structure i've
    defined...

    I've got something like this:
    #define bool short int
    #define TRUE 1
    #define FALSE 0
    #define SMOOTH_FACTOR (3.0 / 4.0)

    typedef struct {
    float x, y, z;
    int r, g, b;
    bool isDefined;
    } Vertex;

    followed by:

    /*
    * plasmaFractal (): Generate random heights for an array of Vertex's
    */
    void
    plasmaFractal (Vertex ** vertices, int xmin, int xmax, int ymin, int
    ymax, float randMax, bool negative) {
    int xmid, ymid;

    xmid = (xmax - xmin) / 2;
    ymid = (ymax - ymin) / 2;

    if ((xmax - xmin) < 2) {
    return;
    }

    /* Set middle value to a random number between 0 and randMax, or
    -randMax and randMax */

    vertices[xmin + xmid][ymin + ymid].z = (vertices[xmin][ymin].z +
    vertices[xmin][ymax].z + vertices[xmax][ymin].z +
    vertices[xmax][ymax].z) / 4.0 +
    randomFloat (randMax, negative);

    /* Now, set the middle vertice values, if not set yet */
    if (vertices[xmin][ymin + ymid].isDefined == FALSE) {
    vertices[xmin][ymin + ymid].z = (vertices[xmin][ymin].z +
    vertices[xmin][ymax].z) / 2.0;
    vertices[xmin][ymin + ymid].isDefined = TRUE;
    }

    if (vertices[xmax][ymin + ymid].isDefined == FALSE) {
    vertices[xmax][ymin + ymid].z = (vertices[xmax][ymin].z +
    vertices[xmax][ymax].z) / 2.0;
    vertices[xmax][ymin + ymid].isDefined = TRUE;
    }

    if (vertices[xmin + xmid][ymin].isDefined == FALSE) {
    vertices[xmin + xmid][ymin].z = (vertices[xmin][ymin].z +
    vertices[xmax][ymin].z) / 2.0;
    vertices[xmin + xmid][ymin].isDefined = TRUE;
    }

    if (vertices[xmin + xmid][ymax].isDefined == FALSE) {
    vertices[xmin + xmid][ymax].z = (vertices[xmin][ymax].z +
    vertices[xmax][ymax].z) / 2.0;
    vertices[xmin + xmid][ymax].isDefined = TRUE;
    }

    plasmaFractal (vertices, xmin, ymin, xmin + xmid, ymin + ymid, randMax
    * SMOOTH_FACTOR, negative);
    plasmaFractal (vertices, xmin, ymin + ymid, xmin + xmid, ymax, randMax
    * SMOOTH_FACTOR, negative);
    plasmaFractal (vertices, xmin + xmid, ymin, xmax, ymin + ymin, randMax
    * SMOOTH_FACTOR, negative);
    plasmaFractal (vertices, xmin + xmid, ymin + ymid, xmax, ymax, randMax
    * SMOOTH_FACTOR, negative);

    }

    (randomFloat generates a random number between -randMax and randMax if
    negative is set to TRUE). My main looks like:

    int main (void) {
    Vertex ** vertices;
    int i, j;
    float randMax = 100.0;
    bool negative = TRUE;
    int xmin, xmax;
    int ymin, ymax;

    xmin = ymin = 0;
    xmax = ymax = 8;

    if ((vertices = (Vertex **) malloc ((xmax + 1) * (ymax + 1) * sizeof
    (Vertex))) == NULL) {
    fprintf (stderr, "malloc(): out of memory.\n");
    exit (EXIT_FAILURE);
    }

    for (i = xmin; i <= xmax; i++) {
    for (j = ymin; j <= ymax; j++) {
    vertices[j].x = (float) i;
    vertices[j].y = (float) j;
    vertices[j].isDefined = FALSE;
    }
    }
    srand (getpid ());

    vertices[xmin][ymin].z = randomFloat (randMax, negative);
    vertices[xmin][ymax].z = randomFloat (randMax, negative);
    vertices[xmax][ymin].z = randomFloat (randMax, negative);
    vertices[xmax][ymax].z = randomFloat (randMax, negative);
    vertices[xmin][ymin].isDefined = vertices[xmin][ymax].isDefined =
    vertices[xmax][ymin].isDefined =
    vertices[xmax][ymax].isDefined = TRUE;


    plasmaFractal ((Vertex **)vertices, xmin, ymin, xmax, ymax, randMax *
    SMOOTH_FACTOR, negative);

    for (i = xmin; i <= xmax; i++) {
    for (j = ymin; j <= ymax; j++) {
    printf ("[%2f]", vertices[j].z);
    }
    printf ("\n");
    }

    exit (EXIT_SUCCESS);
    }

    I get a segfault. Any clues ?
     
    , Apr 11, 2006
    #1
    1. Advertising

  2. pemo Guest

    wrote:
    > Hello,
    >
    > I'm having some trouble with accessing members of a structure i've
    > defined...
    >
    > I've got something like this:
    > #define bool short int
    > #define TRUE 1
    > #define FALSE 0
    > #define SMOOTH_FACTOR (3.0 / 4.0)
    >
    > typedef struct {
    > float x, y, z;
    > int r, g, b;
    > bool isDefined;
    > } Vertex;
    >
    > followed by:
    >
    > /*
    > * plasmaFractal (): Generate random heights for an array of Vertex's
    > */
    > void
    > plasmaFractal (Vertex ** vertices, int xmin, int xmax, int ymin, int
    > ymax, float randMax, bool negative) {
    > int xmid, ymid;
    >
    > xmid = (xmax - xmin) / 2;
    > ymid = (ymax - ymin) / 2;
    >
    > if ((xmax - xmin) < 2) {
    > return;
    > }
    >
    > /* Set middle value to a random number between 0 and randMax, or
    > -randMax and randMax */
    >
    > vertices[xmin + xmid][ymin + ymid].z = (vertices[xmin][ymin].z +
    > vertices[xmin][ymax].z + vertices[xmax][ymin].z +
    > vertices[xmax][ymax].z) / 4.0 +
    > randomFloat (randMax, negative);
    >
    > /* Now, set the middle vertice values, if not set yet */
    > if (vertices[xmin][ymin + ymid].isDefined == FALSE) {
    > vertices[xmin][ymin + ymid].z = (vertices[xmin][ymin].z +
    > vertices[xmin][ymax].z) / 2.0;
    > vertices[xmin][ymin + ymid].isDefined = TRUE;
    > }
    >
    > if (vertices[xmax][ymin + ymid].isDefined == FALSE) {
    > vertices[xmax][ymin + ymid].z = (vertices[xmax][ymin].z +
    > vertices[xmax][ymax].z) / 2.0;
    > vertices[xmax][ymin + ymid].isDefined = TRUE;
    > }
    >
    > if (vertices[xmin + xmid][ymin].isDefined == FALSE) {
    > vertices[xmin + xmid][ymin].z = (vertices[xmin][ymin].z +
    > vertices[xmax][ymin].z) / 2.0;
    > vertices[xmin + xmid][ymin].isDefined = TRUE;
    > }
    >
    > if (vertices[xmin + xmid][ymax].isDefined == FALSE) {
    > vertices[xmin + xmid][ymax].z = (vertices[xmin][ymax].z +
    > vertices[xmax][ymax].z) / 2.0;
    > vertices[xmin + xmid][ymax].isDefined = TRUE;
    > }
    >
    > plasmaFractal (vertices, xmin, ymin, xmin + xmid, ymin + ymid, randMax
    > * SMOOTH_FACTOR, negative);
    > plasmaFractal (vertices, xmin, ymin + ymid, xmin + xmid, ymax, randMax
    > * SMOOTH_FACTOR, negative);
    > plasmaFractal (vertices, xmin + xmid, ymin, xmax, ymin + ymin, randMax
    > * SMOOTH_FACTOR, negative);
    > plasmaFractal (vertices, xmin + xmid, ymin + ymid, xmax, ymax, randMax
    > * SMOOTH_FACTOR, negative);
    >
    > }
    >
    > (randomFloat generates a random number between -randMax and randMax if
    > negative is set to TRUE). My main looks like:
    >
    > int main (void) {
    > Vertex ** vertices;
    > int i, j;
    > float randMax = 100.0;
    > bool negative = TRUE;
    > int xmin, xmax;
    > int ymin, ymax;
    >
    > xmin = ymin = 0;
    > xmax = ymax = 8;
    >
    > if ((vertices = (Vertex **) malloc ((xmax + 1) * (ymax + 1) * sizeof
    > (Vertex))) == NULL) {
    > fprintf (stderr, "malloc(): out of memory.\n");
    > exit (EXIT_FAILURE);
    > }
    >
    > for (i = xmin; i <= xmax; i++) {
    > for (j = ymin; j <= ymax; j++) {
    > vertices[j].x = (float) i;
    > vertices[j].y = (float) j;
    > vertices[j].isDefined = FALSE;
    > }
    > }
    > srand (getpid ());
    >
    > vertices[xmin][ymin].z = randomFloat (randMax, negative);
    > vertices[xmin][ymax].z = randomFloat (randMax, negative);
    > vertices[xmax][ymin].z = randomFloat (randMax, negative);
    > vertices[xmax][ymax].z = randomFloat (randMax, negative);
    > vertices[xmin][ymin].isDefined = vertices[xmin][ymax].isDefined =
    > vertices[xmax][ymin].isDefined =
    > vertices[xmax][ymax].isDefined = TRUE;
    >
    >
    > plasmaFractal ((Vertex **)vertices, xmin, ymin, xmax, ymax, randMax *
    > SMOOTH_FACTOR, negative);
    >
    > for (i = xmin; i <= xmax; i++) {
    > for (j = ymin; j <= ymax; j++) {
    > printf ("[%2f]", vertices[j].z);
    > }
    > printf ("\n");
    > }
    >
    > exit (EXIT_SUCCESS);
    > }
    >
    > I get a segfault. Any clues ?



    Here's one ...

    printf("%p\n", (void *)vertices);

    (*(int *)vertices) = 1;

    for (i = xmin; i <= xmax; i++) {
    for (j = ymin; j <= ymax; j++) {

    printf("%p\n", (void *)&vertices[j]);


    --
    ==============
    Not a pedant
    ==============
     
    pemo, Apr 11, 2006
    #2
    1. Advertising

  3. Pedro Graca Guest

    wrote:
    > I've got something like this:
    > #define bool short int
    > #define TRUE 1
    > #define FALSE 0
    > #define SMOOTH_FACTOR (3.0 / 4.0)
    >
    > typedef struct {
    > float x, y, z;
    > int r, g, b;
    > bool isDefined;
    > } Vertex;


    [snip]

    > main looks like:
    >
    > int main (void) {
    > Vertex ** vertices;
    > int i, j;
    > float randMax = 100.0;
    > bool negative = TRUE;
    > int xmin, xmax;
    > int ymin, ymax;
    >
    > xmin = ymin = 0;
    > xmax = ymax = 8;
    >
    > if ((vertices = (Vertex **) malloc ((xmax + 1) * (ymax + 1) * sizeof
    > (Vertex))) == NULL) {


    [snip]

    > I get a segfault. Any clues ?


    Pointer problems :)

    See FAQ 6.16 <http://www.c-faq.com/aryptr/dynmuldimary.html>
    and, while you're there, see other FAQs.

    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
     
    Pedro Graca, Apr 11, 2006
    #3
  4. CBFalconer Guest

    wrote:
    >
    > I'm having some trouble with accessing members of a structure i've
    > defined...
    >
    > I've got something like this:
    > #define bool short int
    > #define TRUE 1
    > #define FALSE 0
    > #define SMOOTH_FACTOR (3.0 / 4.0)
    >
    > typedef struct {
    > float x, y, z;
    > int r, g, b;
    > bool isDefined;
    > } Vertex;
    >
    > followed by:


    Several suggestions. First, make it easy for someone to extract
    and compile your code. That means limiting line length to about 65
    or so, not using // comments, and ensuring that the code is
    suitably indented and consistently formatted.

    Second, the very presence of "#define TRUE 1" is a red flag. In C,
    an expression is true if it is non-zero. Thus "#define FALSE 0" is
    unlikely to induce errors, but the TRUE value is highly error
    prone. I also don't like the definition of bool, but that is more
    my taste than error probabilities.

    Third, your code should be complete, so it only needs to be copied
    and compiled. Then you can state clearly the input, the
    expectations, and the failures.

    Since you are posting from that broken usenet interface at google,
    read my sig. below and the referenced URLs therein before posting
    again or replying.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Apr 11, 2006
    #4
  5. On 11 Apr 2006 01:07:58 -0700, wrote:

    >Hello,
    >
    >I'm having some trouble with accessing members of a structure i've
    >defined...
    >
    >I've got something like this:


    Show us your real code.

    >#define bool short int
    >#define TRUE 1
    >#define FALSE 0
    >#define SMOOTH_FACTOR (3.0 / 4.0)
    >
    >typedef struct {
    > float x, y, z;
    > int r, g, b;
    > bool isDefined;
    >} Vertex;
    >
    >followed by:
    >
    >/*
    > * plasmaFractal (): Generate random heights for an array of Vertex's
    >*/
    >void
    >plasmaFractal (Vertex ** vertices, int xmin, int xmax, int ymin, int
    >ymax, float randMax, bool negative) {

    snip
    >}
    >
    >(randomFloat generates a random number between -randMax and randMax if
    >negative is set to TRUE). My main looks like:
    >
    >int main (void) {
    > Vertex ** vertices;
    > int i, j;
    > float randMax = 100.0;
    > bool negative = TRUE;
    > int xmin, xmax;
    > int ymin, ymax;
    >
    > xmin = ymin = 0;
    > xmax = ymax = 8;
    >
    > if ((vertices = (Vertex **) malloc ((xmax + 1) * (ymax + 1) * sizeof
    >(Vertex))) == NULL) {


    Don't cast the return from malloc. It doesn't help and can suppress
    warnings you really want to see.

    While this does allocate space for as many structs as you want, it
    does not do it in a way that will let you use the 2-dimensional
    subscript operators. See below.


    > fprintf (stderr, "malloc(): out of memory.\n");
    > exit (EXIT_FAILURE);
    > }
    >
    > for (i = xmin; i <= xmax; i++) {
    > for (j = ymin; j <= ymax; j++) {
    > vertices[j].x = (float) i;


    Here is one segfault. verices is a Vertex**. Therefore, vertices
    must be a Vertex*. That is, it must be a pointer which holds the
    address of a Vertex. You didn't even allocate the correct amount of
    space for a Vertex*

    If you want to use the single malloc approach as above, then vertices
    must be a Vertex* and you access the j-th element in the i-th row with
    vertices[i*(ymax+1)+j].

    If you want to use normal subscripting, then allocate the space with
    something like
    vertices = malloc((xmax+1) * sizeof *vertices);
    for (i = 0; i < xmax+1; i++)
    vertices = malloc((ymax+1) * sizeof *vertices);

    > vertices[j].y = (float) j;
    > vertices[j].isDefined = FALSE;
    > }
    > }
    > srand (getpid ());
    >
    > vertices[xmin][ymin].z = randomFloat (randMax, negative);
    > vertices[xmin][ymax].z = randomFloat (randMax, negative);
    > vertices[xmax][ymin].z = randomFloat (randMax, negative);
    > vertices[xmax][ymax].z = randomFloat (randMax, negative);
    > vertices[xmin][ymin].isDefined = vertices[xmin][ymax].isDefined =
    >vertices[xmax][ymin].isDefined =
    > vertices[xmax][ymax].isDefined = TRUE;
    >
    >
    > plasmaFractal ((Vertex **)vertices, xmin, ymin, xmax, ymax, randMax *
    >SMOOTH_FACTOR, negative);


    What do you think the cast accomplishes?

    >
    > for (i = xmin; i <= xmax; i++) {
    > for (j = ymin; j <= ymax; j++) {
    > printf ("[%2f]", vertices[j].z);
    > }
    > printf ("\n");
    > }
    >
    > exit (EXIT_SUCCESS);
    >}
    >
    >I get a segfault. Any clues ?


    It would be nice if you told us where this occurred.


    Remove del for email
     
    Barry Schwarz, Apr 12, 2006
    #5
  6. On Wed, 12 Apr 2006 00:14:00 -0700, Barry Schwarz <>
    wrote:

    >On 11 Apr 2006 01:07:58 -0700, wrote:
    >
    >>Hello,
    >>
    >>I'm having some trouble with accessing members of a structure i've
    >>defined...
    >>
    >>I've got something like this:

    >
    >Show us your real code.
    >
    >>#define bool short int
    >>#define TRUE 1
    >>#define FALSE 0
    >>#define SMOOTH_FACTOR (3.0 / 4.0)
    >>
    >>typedef struct {
    >> float x, y, z;
    >> int r, g, b;
    >> bool isDefined;
    >>} Vertex;
    >>
    >>followed by:
    >>
    >>/*
    >> * plasmaFractal (): Generate random heights for an array of Vertex's
    >>*/
    >>void
    >>plasmaFractal (Vertex ** vertices, int xmin, int xmax, int ymin, int
    >>ymax, float randMax, bool negative) {

    >snip
    >>}
    >>
    >>(randomFloat generates a random number between -randMax and randMax if
    >>negative is set to TRUE). My main looks like:
    >>
    >>int main (void) {
    >> Vertex ** vertices;
    >> int i, j;
    >> float randMax = 100.0;
    >> bool negative = TRUE;
    >> int xmin, xmax;
    >> int ymin, ymax;
    >>
    >> xmin = ymin = 0;
    >> xmax = ymax = 8;
    >>
    >> if ((vertices = (Vertex **) malloc ((xmax + 1) * (ymax + 1) * sizeof
    >>(Vertex))) == NULL) {

    >
    >Don't cast the return from malloc. It doesn't help and can suppress
    >warnings you really want to see.
    >
    >While this does allocate space for as many structs as you want, it
    >does not do it in a way that will let you use the 2-dimensional
    >subscript operators. See below.
    >
    >
    >> fprintf (stderr, "malloc(): out of memory.\n");
    >> exit (EXIT_FAILURE);
    >> }
    >>
    >> for (i = xmin; i <= xmax; i++) {
    >> for (j = ymin; j <= ymax; j++) {
    >> vertices[j].x = (float) i;

    >
    >Here is one segfault. verices is a Vertex**. Therefore, vertices


    Obviously that should be vertices.

    >must be a Vertex*. That is, it must be a pointer which holds the
    >address of a Vertex. You didn't even allocate the correct amount of
    >space for a Vertex*



    Remove del for email
     
    Barry Schwarz, Apr 13, 2006
    #6
    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. Walter Deodiaus

    accessing inner struct members

    Walter Deodiaus, Dec 29, 2005, in forum: C++
    Replies:
    2
    Views:
    339
    Old Wolf
    Dec 29, 2005
  2. Replies:
    3
    Views:
    294
  3. Mark Shelor

    Accessing C struct members via T_PTROBJ

    Mark Shelor, Oct 24, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    133
    Tassilo v. Parseval
    Oct 25, 2003
  4. John Reye
    Replies:
    28
    Views:
    1,377
    Tim Rentsch
    May 8, 2012
  5. Mark
    Replies:
    6
    Views:
    394
    88888 Dihedral
    Jul 9, 2012
Loading...

Share This Page