clearing a used array

This is a discussion on clearing a used array within the C Programming forums, part of the General Programming Boards category; Hi, is there a way to re-initilise an array to allow it to be empty again .. this is what ...

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

    clearing a used array

    Hi,

    is there a way to re-initilise an array to allow it to be empty again ..

    this is what i have done, but my output, doesnt clear it..

    Code:
    for(j=0; j<MAX_LEN; j++)
           { 
              buff[j] = ' '; //flush the strings. .
              printf("buff[%c] - buff", buff[j]);
           }
    this is my output, from file

    Customer No: Fri May 21 23:58:09 2004

    Customer 100: deposit

    Arrival time: Fri May 21 23:58:09 2004

    Customer 2112 : deposit

    Arrival time: Fri May 21 23:58:09 2004

    Customer 3112 : withdraw

    Arrival time: Fri May 21 23:58:09 2004

    Customer 4112 : deposit

    Arrival time: Fri May 21 23:58:09 2004

    Customer 5112 : withdraw

    Arrival time: Fri May 21 23:58:09 2004

    the input is

    100 deposit
    2112 deposit
    3 withdraw
    4 deposit
    5 withdraw

    so basically 3112 is from the old inputs . .

    Please help

    Matt

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Dont know about your inputs, but the output of you code snippet should be this
    "buff[ ] - buff"
    until MAX_LEN is met.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Code:
    #include <stdio.h>
    #include <strings.h>
    
    int main()
    {
    	char buf[1024] = "Hello, full string here!";
      	fprintf(stdout, "%s\nGive Me Input!!\n", buf);
            buf[0] = '\0';
    	if (fgets(buf, sizeof(buf), stdin) == NULL) {
    		fprintf(stderr, "fgets: Failed.\n");
    		return 1;
    	}
    	fprintf(stdout, "Your new string is: %s\n", buf);
    	return 0;
    }
    Just null terminate the first element. : )
    Last edited by loopy; 05-21-2004 at 11:29 AM.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    memset( array, 0, arraysize );
    [edit]*slaps forehead*[/edit]

    Quzah.
    Last edited by quzah; 05-21-2004 at 03:10 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Do you even NEED to clear an array? It seems to work to just write over it...
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    23
    ok this is my function at the moment ..

    I still cannot get it to clear the buffer correctly . .

    Code:
        while (fgets(buff, MAX_LEN, fin) != NULL)
    
        {
    
           char custAction[MAX_LEN];
           char custRef[MAX_LEN];      
    
           int i=0;
           int x=0;
    
           while((buff[i] != ' ') && (i <= MAX_LEN))
           { //read from buffer storing in custRef values up till
             //a ' ' is found
    
             custRef[i] = buff[i];
               i++;
           }
    
           i++; //increment i past the space
    
           while((buff[i] != ' ') && (i <=MAX_LEN) && (buff != NULL))
           { //read the rest of buffer till next space or EOF
               custAction[x] = buff[i];
              
               i++;
    	   x++;
           }
    
           //printf("custAction is equal to -  %s\n", custAction);   
           
           
                        
           //for(j=0; j<MAX_LEN; j++)
           //{ 
           //   buff[j] = ' '; //flush the strings. .
           //}
    
           memset(buff, 0, MAX_LEN); //reset buffer
    
           printf("%s/n", buff);
           time(&now);
    
           fprintf(fout,"Customer %s: %s\nArrival time: %s\n", custRef, custAction, ctime(&now));
           sleep(2);
        }
    i'm using the memset correctly .. so i figure it is in the wrong spot.. but i cannot find where it should be placed..

    Matt

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    buff != NULL
    should be buff[i]!='\0'
    Code:
    { //read the rest of buffer till next space or EOF
    It isn't EOF it is NULL you even wrote it there. \0 is the string version of NULL. Everything else seems fine, save the one line comments

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    23
    Quote Originally Posted by linuxdude
    Code:
    buff != NULL
    should be buff[i]!='\0'
    Code:
    { //read the rest of buffer till next space or EOF
    It isn't EOF it is NULL you even wrote it there. \0 is the string version of NULL. Everything else seems fine, save the one line comments
    But this still does not solve the problem of my buffer..

    I am still getting reminents of the old custRef on the next pass ..

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by linuxdude
    Everything else seems fine, save the one line comments
    Not quite.

    Code:
    while((buff[i] != ' ') && (i <= MAX_LEN))
    That's wrong. If i is MAX_LEN, then it is beyond the end of the buffer, which is a BadThingTM.

    Additionally...
    Code:
     while((buff[i] != ' ') && (i <=MAX_LEN) && (buff != NULL))
    Should be...
    Code:
     while((buff[i] != ' ') && (i <=MAX_LEN) && (buff[i] != NULL))
    Because... "buff" never changes. It is always considered a pointer to the first element of the array. As such, since it is an array that by definition always exists, even if you have the value of buff[0] as zero (NULL), the array itself, and as a result, the pointer to its first element will still never be NULL.

    Also...
    Code:
    printf("%s/n", buff);
    Your backslash has fallen over on its face.

    Quzah.
    [edit]fixed color tag[/edit]
    Last edited by quzah; 05-22-2004 at 02:04 AM.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    23
    I solved the problem . .

    I needed to clear custRef as well as buff..

    so i print to the file and then clear the buffer.. works a treat now ..

    On to my next problem.. Msgget and semaphores . .

    ARGHH!!

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Arg I caught the buff[i] prob but I glanced over the = so sue me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  2. clearing array
    By suzanne_lim in forum C Programming
    Replies: 14
    Last Post: 04-28-2006, 02:54 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 01:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21