Thread: Using arrays?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Using arrays?

    Im making a database program, and I think I can use an array for it.

    Rather than list the whole specification, ill just use the adding a record function as an example.

    I need to add a record, where each record consits of :

    Code:
    typedef struct {
    
         char surname[21],   /* student surname  */
    
         char firstname[21], /* buffer to receive message */
    
         char studentID[7],  /* student identifier */
    
         int  studentAge     /* age of student in years */
    
         } STUDENT_RECORD;
    How do I create a suitable array (?) for storing an undefined amount of records?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's easy in C++, but in C, you need to be mindful of how many elements in your array you have.
    Start by allocating a number of elements with malloc, and keep the size known!
    Every time you add a record, make sure you have enough space in your array to store the record. If not, use realloc to increase the space.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    With dynamic memory allocation? Resize an array of records as you need them is one approach, see malloc() and realloc().

    Or allocate a reasonable amount of records, with a limit on the amount of records that your program could handle.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by zacs7 View Post
    Or allocate a reasonable amount of records, with a limit on the amount of records that your program could handle.
    Now that's silly and not very dynamic at all. Certainly not the way a database works.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ooh, but if it's for a school project -- and the school project said, "Timmy only needs to store up to 50 students". Then it'd be a goer, it's only a possible suggestion

    Didn't say it had to be dynamic - just '[a] suitable array'.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well,

    Im making a database program, and I think I can use an array for it.
    Making a database program requires a dynamic array, no?

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I see your point, still doesn't eliminate my possible solution, no?
    You could also just store it in a file -- no DMA required

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not necessarily. You could, for example, store all your data in a database file, and not store more than the single entry from the database you are currently looking at in memory.

    But you could also build a database where you store ALL entries in memory, and only store to disk when you finish - depends on your approach.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by zacs7 View Post
    I see your point, still doesn't eliminate my possible solution, no?
    You could also just store it in a file -- no DMA required
    How about this then?
    How do I create a suitable array (?) for storing an undefined amount of records?
    Note the word "undefined."

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > and I think I can use an array for it.

    Note the word think

    Yes yes, no arguing. You win, you're member of the month for next month.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of coruse, you can't create an array to store an undefined number of entries. You can have an array that you re-size using realloc, but it will, at any given time, always have a set size.

    [This also applies in C++, it's just more hidden when you use things like std::vector].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by zacs7 View Post
    Yes yes, no arguing. You win, you're member of the month for next month.
    No, you're the member of the month. You have the title. Don't worry about that, you can have it
    So now I suppose the OP needs to specify a little exactly what the intension is, what the program should do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM