432,188 Members | 844 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

# Accessing members of a struct

 P: n/a 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[i][j].x = (float) i; vertices[i][j].y = (float) j; vertices[i][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[i][j].z); } printf ("\n"); } exit (EXIT_SUCCESS); } I get a segfault. Any clues ? Apr 11 '06 #1
5 Replies

 P: n/a jo*************@gmail.com 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[i][j].x = (float) i; vertices[i][j].y = (float) j; vertices[i][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[i][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[i][j]); -- ============== Not a pedant ============== Apr 11 '06 #2

 P: n/a jo*************@gmail.com 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 and, while you're there, see other FAQs. -- If you're posting through Google read Apr 11 '06 #3 