Thread: Junk Characters

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    56

    Junk Characters

    When stuffing my multidimensional array with characters, I found out that the array is stuffed all the way to its last amount of storage. For example. If I had an array blah[1][8], and the word I stuffed was "Jon" (with the \0 character), then it would print out (if say the * was the junk character because I can't print the character it is showing) "****Jon" if I did something like

    Code:
    x = 1;
    printf("%s", blah[x]);
    I could print it out using a For loop, however, I want the value of blah[x] to not have any junk/null characters.

    I there any way to make it so that the array isn't stuffed from the back way? Any ways to alter this in code?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean
    Code:
    for(y = 0; y < ARRAY_HEIGHT; y ++) {
        for(x = 0; x < ARRAY_WIDTH && array[x]; x ++) {
            putchar(array[x]);
        }
    }
    ?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Wait, my previous post was probably wrong . . . .

    What do you mean by stuff? Does one of your functions do it? strcpy()?

    Is your string NULL-terminated?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by johnchain
    When stuffing my multidimensional array with characters, I found out that the array is stuffed all the way to its last amount of storage. For example. If I had an array blah[1][8], and the word I stuffed was "Jon" (with the \0 character), then it would print out (if say the * was the junk character because I can't print the character it is showing) "****Jon" if I did something like

    Code:
    x = 1;
    printf("%s", blah[x]);
    I could print it out using a For loop, however, I want the value of blah[x] to not have any junk/null characters.

    I there any way to make it so that the array isn't stuffed from the back way? Any ways to alter this in code?
    Arrays start at 0 for indexing, not 1. Therefore, x shouldn't be 1, because that's beyond the end of your valid array indexes. Try staying within the bounds of your array, and see if that helps not have "junk characters".


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post a compilable sample of code that shows your problem.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char buf[1][8];
      strcpy (buf[0], "hello");
      puts (buf[0]);
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    Well, this relates back to my URL finder....

    Code:
       fp1 = fopen(filepath, "rb");
    
      fread(html, sizeof(char), SIZE, fp1); 
     
      
      for (State = NOT_INURL; c = html[i]; i++)
      { 
        
        if (c == '\"') 
        { 
         count++;
    	 State = !State; 
          continue; 
        } 
        if (State == INURL) 
        { 
    	
    		url[b][d] = c;
    		printf("%c", url[b][d]);
    		d++;
    	} 
    	if (count == 2)
    	{
    		url[b][d] = '\0';
    		counter++;
    		printf("\n");
    		b++;
    		count = 0;
    	}
      } 
      
      fclose(fp1); 
    
      seedrnd();
      
      x = rnd(counter);
      
    
      printf("x is equal to %d which was %s\n", x, url[x]);
    As you can see, it just finds a random index and and prints it. Don't worry about the other functions like rnd() and and variables like counter. I take it that I am putting the characters into the array wrong or something. The url array is [150][512]. the value of url[x] is equal to a bunch of junk/null characters, the string, and the null terminator, all concatated into one.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for (State = NOT_INURL; c = html[i]; i++)
    Are you sure that's what you want?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    yes. c= html[i], reads in the current character into i. Could that be the problem?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, I was thinking you meant
    Code:
    c == html[i]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    Oh, no, I want the value of html[i] put into c so I can manipulate c, not html[i]. There must be something that's stuffing the array with characters, putting it in backwords or something like that..

    Edit: Nevermind, I found the mistake. I forgot to reset the second index back to zero, after the string was null terminated.

    Thanks for the help guys.
    Last edited by johnchain; 10-12-2005 at 04:52 PM.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oh . . . okay.

    One other thing: you could eliminate that continue by making the ifs else ifs.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM