This topic is now archived and is closed to further replies.


converting to a constant

Recommended Posts

I have been working on a simple program that reads a number from a text file and then implements that number into an array bracket... example: File>>chOut; scanf(chOut,"%d",&tri); TRI*TRIptr[tri]; The problem is that ''tri'' must be a constant, right? Please show me how to convert tri to a const. Thank ya... -Later

Share this post

Link to post
Share on other sites

What you''re trying to do can''t be done. Not like this.
That is, if I understood correctly what you''re trying to do...

However, something like this might work;

int tricount = atoi ( string_from_file );

TRI *triangles = new TRI[tricount];

// okay, you could use
// TRI *triangles = (TRI*)malloc(tricount*sizeof(TRI));

TRI *tri_number_0 = triangles[0];
TRI *tri_number_1 = triangles[1];

Or possibly

std::vector<trI*> triangles(tricount);

TRI *tri_number_0 = triangles[0];

Preferably the latter. It at least fails properly if (when) you screw up...

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Odd... you''re mixing c++ file operations (File>>chOut) with c file operations (scanf(chOut,"%d",&tri). I have no clue why :o).

FILE *in;
int tri;
TRI *TRIptr;

in = fopen("trifile.txt","r");

//Here you can either

TRIptr = new TRI[tri];
//And when done, remember to delete!!

delete TRIptr;


//Allocate memory - must include something to use this.. but I forgot the .h file.. might try memory.h or malloc.h or something similar

TRIptr = (TRI*)malloc(tri*sizeof(TRI));
//Free memory


Personally I''d stick with the "new" and "delete" operators for memory allocating/deallocating.

Billy -

Share this post

Link to post
Share on other sites