
Newbie question: Making arrays bigger
GMT in comp.lang.c.
Newbie question: Making arrays bigger's a cool scene! Dig it!
Quote:
>I have a requirement for a table of names/phone numbers. Basically this is a
>struct of char name[80] and char telno[30], called dataLine. I have an array
>of 100 dataLines, which is a simple phone book.
>Question: the "100" is totally arbitrary. The number of entries could be 10,
>or 100,000. The data file can be reloaded while the program is running to
>update the database. How can I make my array bigger if, say, someone's added
>100 phone numbers to the file? I don't want to specify a very big arbitrary
>maximum and hope for the best, because of the huge possible range. I'd much
>rather the program increased the array size to cope.
The simple answer would be to use realloc() to re-allocate the array
every time it needs to change size. However, this may not be the most
efficient method, and is not usually what the professionals do.
The most common method for doing this is to use a "linked list".
This is a structure that can store any number of items (within memory
limits, of course). Each item within a linked list, called a "node",
contains not only the all important data itself, but also a pointer to
the next node. A node of a double linked list also has a pointer to
the previous node. Each node is dynamically allocated as it is needed
and freed when it is no longer required.
Linked lists are a rather advanced topic, though not really all that
hard a concept to understand in its basic form. If you want to know
more about this subject, I suggest you find a good book on data
structures and such.
Quote:
>Please respond by e-mail. I will post a summary of responses later.
No.
--
----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----
-------------- Shaggy was here! ---------------
http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============