Thread: null terminated

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    null terminated

    I've read that a string always has to be NULL terminated, so I decided to fool around a little with it.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char naam[20];
        int x;
        printf("Voer naam in: ");
        scanf("%20s", &naam); 
        
        if (strlen(naam) > 19)
        {
            printf("String te lang (max 20).. hij zal ingekort worden.\n\n");
        }
        for (x = 0; x <= 20; x++)
        { 
            printf("karakter %d: %c\n", x, naam[x]);
        }
        
        naam[20] = 'd';
        for (x = 0; x <= 20; x++)
        { 
            printf("karakter %d: %c\n", x, naam[x]);
        }    
        printf("naam: %s", naam);
    }
    as you can see naam[20] is filled with d at the end, why is that accepted, now theres now the string isn't terminated, isnt this potentialy dangerous?

    I dunno maybe Im just talking bull$$$$ & newbie like, but im curious.
    thanx

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>naam[20] = 'd';
    This is wrong. The indices in naam go from 0 to 19, not from 0 to 20.

    So in your for loops, it should be x < 20.

    >>isnt this potentialy dangerous?
    Indeed it is, which is why you should always make sure your strings are null-terminated. Otherwise, output methods don't know where to stop.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    67
    all right

  4. #4
    Banned
    Join Date
    May 2003
    Posts
    124
    There is a problem with your
    Code:
     for (x = 0; x <= 20; x++)
    {
    printf("karakter %d: %c\n", x, naam[x]);
    }
    if the input is less than 19 characters long.

    It should be x< strlen(naam) (not even x<20 )
    Code:
    naam[20] = 'd';
    	for (x = 0; x <= 20; x++)
    		  printf("karakter %d: %c\n", x, naam[x]);
    	 printf("naam: %s", naam);
    doesn't work at all.

    You also forgot to return 0; in function main().

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    67
    it works and outputs like this:
    Code:
    C:\Program Files\Dev-Cpp\Crypt>stringtest
    Voer naam in: test
    karakter 0: t
    karakter 1: e
    karakter 2: s
    karakter 3: t
    karakter 4:
    karakter 5: ▲
    karakter 6: Φ
    karakter 7: w
    karakter 8: ♀
    karakter 9:
    karakter 10:
    karakter 11:
    karakter 12: ☻
    karakter 13:
    karakter 14:
    karakter 15:
    karakter 16: `
    karakter 17: _
    karakter 18: "
    karakter 19:
    karakter 20:
    karakter 0: t
    karakter 1: e
    karakter 2: s
    karakter 3: t
    karakter 4:
    karakter 5: ▲
    karakter 6: Φ
    karakter 7: w
    karakter 8: ♀
    karakter 9:
    karakter 10:
    karakter 11:
    karakter 12: ☻
    karakter 13:
    karakter 14:
    karakter 15:
    karakter 16: `
    karakter 17: _
    karakter 18: "
    karakter 19:
    karakter 20: d
    naam: test
    that wasnt the point.. i was wondering about the null termination.

    and you were right i forgot return 0;

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: null terminated

    Originally posted by TeQno
    printf("karakter %d: %c\n", x, naam[x]);
    For additional understanding, change your print statement to:
    printf("karakter %d: [%c] %02X\n", x, naam[x], naam[x]);

    This way if the character is not printable, you'll see that immediately, and also display the HEX value of the character being printed.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  3. Menu like file, edit, help, etc...
    By Livijn in forum Windows Programming
    Replies: 44
    Last Post: 01-23-2007, 05:49 PM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM