I have a question about arrays. I know that if you
declare an array like this for example: char array[]={a,b,c,d}; The size of the array will be automatically 4.
What would you do for a for a 2 dimensional array(or any array) if you
don't know how big the array should be declared
as? For example, I am trying to read a file of 5
character words from a file into an array. I
declared this: char words[][5]; I don't know if the file contains 50 words or 5000. Thanks for any suggestions!
Comments
: declare an array like this for example: char array[]={a,b,c,d}; The size of the array will be automatically 4.
: What would you do for a for a 2 dimensional array(or any array) if you
: don't know how big the array should be declared
: as? For example, I am trying to read a file of 5
: character words from a file into an array. I
: declared this: char words[][5]; I don't know if the file contains 50 words or 5000. Thanks for any suggestions!
Well, you can't leave both dimensions of a multiply-dimensioned array blank. Only the last one can be undimensioned to do aggregate-initialization as you did above. However, single or double or triple dimension arrays aside, you can't change the size of a standard array at run time.
You can either have an array which is large enough to store a maximum number of elements, or you can dynamically allocate an array using the new-array syntax (with the corresponding delete-array syntax to clean up), or you can go to a different type of data structure (like a linked list or a tree of some sort).
Arrays, even dynamically allocated ones, are random access. Relatively easy to access, but if you need to increase or decrease the size of a dynamically allocated array you have to jump through a few hoops (you have to allocate a new array, copy over the data you want to keep, and delete the old array).
Linked lists are linear-access (from the start of the list down), but are easier to add to or remove from because you can do so without upsetting the rest of the list much. They're also a lot faster to grow and shrink.
There are a bazillion different types of trees that I won't go into here that have different properties for sorting and organization...