array question...

This is a discussion on array question... within the C Programming forums, part of the General Programming Boards category; I have a character array that I am using in a loop. The array is used to store names. I ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    77

    array question...

    I have a character array that I am using in a loop. The array is used to store names. I can't figure out how to clear it for the second and subsequent times when it loops. The problem is that the old names are still in the array and they are being processed again. Here is what I mean:

    The array looks like this:
    char names[40][20];

    when I run the program I load 15 names into the array then process those names. I then loop again and load in 10 names, except the last 5 names from the previous pass are still there and get processed.


    Is there a way to clear this array?

    Thanks in advance!

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    for (int i = 0; i < 40; i++)
    {
         for (int j = 0; j < 20; j++)
         {
              char names[i][j] = '\0';          //Or whatever "null character" you want
         }
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Keep an accurate count of how many names you read in on the second pass.
    Then always use that count when deciding to sort / print / whatever with the array of names.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    I tried what you suggested and it won't compile. My array is declared as follows:

    Code:
    	typedef struct start_info
    	{
    	 char name[20];
    	 int age[10];
    	}start_info;
    
    	start_info start[40];
    I like the idea of keeping count of the number of names. I am using sccanf to store the names into the array. Is there a way to keep count of each name? My code that stores the names looks like this:

    Code:
    	while (fgets(line,sizeof(line),f)!=NULL)
    	{
    	  int n;
    	  char *p = line;
    
    	  pc = strstr (line,"AGES");
    	  if (pc != NULL)
    	  {
    		 //printf("%s\n", line);
    		 break;
    	  }
    
    	  while ( i < 20 && sscanf( p, "%s%n", start[i].name, &n) == 1 )
    	  {
    		  p += n;
    		  i++;
    	  }
    	}
    Last edited by jerrykelleyjr; 11-14-2006 at 04:03 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    maybe

    start[ i ].name

    Gotta get the right array reference in there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    Salem you're right I copied it wrong...it looks like you said.

    OK it now compiles...I had "\0". Another fubar.
    Last edited by jerrykelleyjr; 11-14-2006 at 04:07 PM.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    can do something like this as well

    Code:
    for(i=0;i<MAXVALUE; i++)
    {
         fgets(start[i].name, strlen(start[i].name,stdin);
         fgets(start[i].age, strlen(start[i].age, stdin);
    }
    ssharish2005

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    how can I count how many names I have?

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Well, assuming you use the code I posted at the very start, you can test if the first element equals null, if so, the name is unused.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by jerrykelleyjr
    how can I count how many names I have?
    u are using structs, so every struct has a name char array with in it.

    if u declare a array of that struct or something like this
    Code:
    struct somestruct start[10];
    obvisouly u know that there are 10 name cos i have declared 10 elements of type struct somestruct.

    or there might be a case where u wont enter a name for some records. At that time u could store null char in all the name[0] in each record. Something like this

    Code:
    for(i=0;i<10;i++)
        start[i].name[0] = '\0';
    and u can check for
    Code:
    if(name[0] == '\0')
       then u haven;t entered name
    else
       name enetred
    ssharish2005

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Please use proper English: you, not u.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by manutd
    Please use proper English: you, not u.
    thats what i am using from years. I am trying i will very soon

    ssharish2005

  13. #13
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Just a matter of preference
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    77
    Thanks guys/gals....be nice.

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    We are. Not being nice:

    What are you thinking!!!??? USE ENGLISH! ITS YOU NOT U! AAAAAAAAAAAAAAAAAAAAH!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

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