Thread: How to make unknown size arrays?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    42

    How to make unknown size arrays?

    How would I go about making an array that increases it's own size if needed (not unlike 'char *argv[]')?

    Let me give you a bit more info. I'm trying to fread() a text document into an array, but of course text documents vary in content. So if I set, say 'char array[50]' then it will only be able to take 49 chars. So I was wondering if it was possible to either:

    1. Make an 'unknown size' array, that increases as needed.
    2. Make an array, then it increases itself as needed (like Add() in MFC)

    I've tried:
    Code:
    char array[];
    and
    Code:
    char *array[];
    to no avail. Any help would be much appriecated.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The only thing close to what you're asking for is a linked list.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you really wanted to you could use pointers and dynamic allocation to simulate resizable arrays. realloc is especially useful in this. However, that's a real pain in the butt, so why not just use a linked list as suggested.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    Linked lists, hmm, nice tutorial on this site for it.

    Thanks guys.
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  5. #5
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    could you please post the link to it. I Couldn't find it in the faq or tutarials section. (never looked into how to make a linked list usaly use a malloc and realloc instad)
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I Couldn't find it in the faq or tutarials section.
    http://www.cprogramming.com/tutorial/lesson15.html
    My best code is written with the delete key.

  7. #7
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    thanks
    for the help
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unknown size array
    By archriku in forum C Programming
    Replies: 14
    Last Post: 05-07-2009, 11:29 PM
  2. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  3. Replies: 4
    Last Post: 04-05-2004, 06:49 AM
  4. size of integer arrays
    By steve8820 in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 07:31 PM