Results 1 to 5 of 5

Thread: Array Questions

  1. #1

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200

    Array Questions

    O.K. say i have a char array but i do not know how many characters are going to be inputed from the user. Is there a way that i can declare an array and then edit how many places i need on the fly? Also say i declare an array say....char mychar[10]; ......but come to find out that for some reason i need to have an extra place for something. Is there a way that i can stuff something in it?

    Thanks!

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    For the first one, use std::string. For a more general solution, a std::vector is a dynamic array.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    A static array is static. No way to change its size.
    You can use dynamic arrays with new[] but you'll have to watch the size of the array yourself. Also most input functions don't tell you when the user enters too much, which means that you have to read character by character.
    Growable array classes like std::vector take the memory management away from you. However you still can't properly read in, unless you use instream iterators, which is a rather advanced C++ topic.
    The std::string class finally does everything for you. Memory management, size watching and all the stuff that makes C so hard for newbies are all well hidden.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    say just for a learning experience i wanted to make my own class that would create strings.

    Could someone let me know how exactly the string class works. I mean how does it take a character and put it in memory and then how does it keep up w/ all the different characters and put them into a string.

    I know that there is already a string.h file out there but say i wanted to create my own file for creating a string from characters. how would i need to go about it? WHere would i need to start?

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    A simple string class just allocates a character array via new[] and stores the characters there. It also stores its own length so that it doesn't need NUL-terminated strings.
    Then it provides member functions and overloaded operators to manipulate the string, which make it grow and shrink as necessary.

    std::string might have various optimizations: VC++7's implementation for example has a char[16] member to avoid heap allocations for very small strings.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width