Thread: question on NEEDING constants to declare an array of structures

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    question on NEEDING constants to declare an array of structures

    sorry to ask another question, but i need to use a struct, and am declaring it like this...

    Code:
    struct Transaction	// Declaration of Transaction Structure
    {
        int ordernumber;
    	char date[10];
    	int orderedproductid;
    	float priceperproduct;
    	int numberordered;
    	char productlineid[10];
    	int customerid;
    };
    
    and.... this is where i use it... first is a counter to find how many
    lines are in the file, so thats how i know how large to declare my records.
    
    while( (ch = transactionin.peek()) != EOF)  // this counts how many lines are
    {								// in the transaction.h data file
    	transactionin.getline(line,100,NEWLINE);
    		LINECOUNTER++;
    }	
    
    const int MAXRECS = LINECOUNTER;	//used to declare amount of space in the structure
    Transaction records[MAXRECS] ;	//declaring the structure with MAXRECS number
    thats only part of my code, the counter works fine, but it says my maxrecs needs to be a constant, and i can not get this to work since i need to set maxrecs equal to the linecounter from above.

    thanks

    Jon

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try using dynamic memory allocation if you want run-time array sizes.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    thanks for responding. i am reading up all about dynamic mem. allocation, but am having trouble with it.

    how will i use it for my particular case? id imagine for my purposes its a pretty simple thing to do

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    use the "new" key word for dynamic memory allocation. remember that this will return a pointer to your array, not the array itself.

  5. #5
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448

    Something like...

    something like this:

    transaction* records = new transaction[MAXRECS];

    NOTE: you could use LINECOUNTER directly thus saving some memory.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  2. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  3. Yet another array question...
    By Raigne in forum C++ Programming
    Replies: 11
    Last Post: 11-13-2008, 01:55 PM
  4. pointer/ array question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 05:06 PM
  5. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM