Thread: clearing arrays

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    16

    clearing arrays

    just a quick one!

    How do u clear an array of all its data? ive just got random stuff in and i cant get rid of it!

    heres my structure

    Code:
    class book {
    	//starting the class
    public:
    	struct detail {
    	//structure start
    	char name[20];
        char authors[20];
    	char ISBN[20];
    	char publisher[20];
    	char cdrom[20];
    	char year[20];
    	char price[20];
    	//fields in structure
    	}np[MAX];
    
    
    
    void enter(int);
    void finding(void);
    void open(int);
    void findnme(void);
    void findauth(int);
    void findpub(int);
    void findyear(int);
    void findcd(int);
    	//defining things in base class for the search function
    };
    thanks in advance! im sure its simple but i cant see it!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    char string[80];
    for (int i = 0, i < 80, i++)
    {
        string[i] = ' ';
    }
    This fills the string with spaces - you could put any character you want in between the single quotes. You might use nulls ( '\0' ).

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    16
    where would this go? with the rest of the char's? or after np[MAX]?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It's code. Instructions. You just do it somewhere in your code before you use the array. if this structure is global, I would do it at the beginning of your main() function.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    16
    thanks!

    saved my life!

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Code:
    void * memset ( void * buffer, int c, size_t num ); //to initialize a buffer with a specific value.
    Last edited by manofsteel972; 08-22-2004 at 05:19 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some clearing out on this - (Arrays)
    By (Slith++) in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2005, 05:54 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. clearing char arrays
    By gell10 in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 06:03 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM