Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2D array problem (pointers, malloc?)

Status
Not open for further replies.

beezo

Programmer
Oct 14, 2003
3
AU
I have a function that fills a 2D array with verticies but i want the 2D array to be declared outside the function, where the size is unknown. so the function only manipulates the array.

I don't know what parameter to pass in, or how to declare the function. Can someone have a read, and tell me the right (better) way to do it?

what i've got so far:


typedef struct {
float x, y, z;
} VERTEX;

VERTEX **points;

void build_shape( float fi, float theta ) {
float fi;
float theta;
float delta = PI / scale;

points = (VERTEX**)malloc(sizeof(VERTEX)*(scale*2+1));
/* as in: VERTEX points[scale*2+1][scale*2+1] */

VERTEX v;
v.x = 0; /* initialisation */
v.y = 0;
v.z = 0;

if( surface == WIREFRAME ) {
/* horizontal lines in the wire-frame rendering */
for( fi = 0.0; fi <= 2*PI; fi += delta ) {
for( theta = 0; theta <= 2*PI; theta += delta ) {
calc_shape( fi, theta, &v );
points[fi/(PI/denom)][theta/(PI/denom)] = v;
}
}

/* vertical lines in the wire-frame rendering */
for( theta = 0.0; theta <= 2*PI; theta += delta ) {
for( fi = 0.0; fi <= 2*PI; fi += delta ) {
calc_shape( fi, theta, &v );
points[fi/(PI/denom)][theta/(PI/denom)] = v;
}
}
}
}
 
Well I could see one problem, which you can correct

void build_shape( float fi, float theta ) {
// float fi; Dont define this again
// float theta; Dont define this again

One thing I count not understand was where are you getting this &quot;scale&quot; from. Is this also a global variable or what.

Can you please elaborate your problem a bit more.
 
you are right, that was just a typo.

denom is a global variable, a float; scale is a mistake, scale is denom.
PI is defined as mathematical PI.

corrected version so far, below.

what more can i say about this problem? basically, i have a 2D array points[ROW][COL], where size of ROW and COL = denom*2+1

i want the function to return the 2D array, but I thought using pointers would be better, since the size of the 2D array is not known at first.

e.g.

VERTEX points[denom*2+1][denom*2+1]; /* denom changes at runtime */
points = build_shape(fi, theta);

would become something like:
VERTEX **points;

...build_shape( float fi, float theta ) {
points = (VERTEX**)malloc((denom*2+1) * (denom*2+1) * sizeof(VERTEX) );
...
}
 
#define PI 3.141592654
#define WIREFRAME 1

typedef struct {
float x, y, z;
} VERTEX;

VERTEX **points;
float denom;
int surface;

void build_shape( float fi, float theta ) {
float delta = PI / denom;

points = (VERTEX**)malloc(sizeof(VERTEX)*(denom*2+1));
/* as in: VERTEX points[denom*2+1][denom*2+1] */

VERTEX v;
v.x = 0; /* initialisation */
v.y = 0;
v.z = 0;

if( surface == WIREFRAME ) {
/* horizontal lines in the wire-frame rendering */
for( fi = 0.0; fi <= 2*PI; fi += delta ) {
for( theta = 0; theta <= 2*PI; theta += delta ) {
calc_shape( fi, theta, &v );
points[fi/(PI/denom)][theta/(PI/denom)] = v;
}
}

/* vertical lines in the wire-frame rendering */
for( theta = 0.0; theta <= 2*PI; theta += delta ) {
for( fi = 0.0; fi <= 2*PI; fi += delta ) {
calc_shape( fi, theta, &v );
points[fi/(PI/denom)][theta/(PI/denom)] = v;
}
}
}
}
 
If you want to dynamically allocate
Code:
VERTEX points[X][Y];
, then you do this

Code:
int col;
VERTEX **points;
points = malloc( X * sizeof(VERTEX*) );
for ( col = 0 ; col < X ; col++ ) {
  points[col] = malloc( Y * sizeof(VERTEX) );
}

// use points[x][y] as normal


// free
for ( col = 0 ; col < X ; col++ ) {
  free( points[col] );
}
free( points );


--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top