Thread: Simple Program, null terminator

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    7

    Simple Program, null terminator

    Hey, i'm trying to learn C and am stuck trying to answer a simple question. ( Copying sequence of chars )

    My Problem is that my program is printing out a random symbol afterwards , when trying to copy a sequence of chars into a new buffer.


    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    int tokenCopy(char* dest, const char* src, int destSize);
    
    
    int main()
    {
        char buff[3];
        int n = tokenCopy(buff, "This is a string", 3);
        printf("%d '%s'\n", n, buff);
        
        return EXIT_SUCCESS;
    }
    int tokenCopy(char* dest, const char* src, int destSize){
        
        int num_chars = 0;
        
        
        for(int i = 0; i < destSize; i++){
            
            if(src[i] == '\0' || src[i] == ' '  ){
            break;
            }
            
            
            dest[i] = src[i];
            num_chars+= 1;
            
            
            
        }
        
        return num_chars;
    }
    Could someone tell me what i need to change? I think its something to do with null terminator?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Correct you are!

    If the dest size is 3, then you need to copy just two chars, leave the for loop, and add
    Code:
    dest[i]='\0';
    Just after the for loop in tokenCopy(). NOW you have a string. Without that, you don't have a string - (unless C adds the end of string marker char for you). You have just a bunch of chars - not a string - and they won't print correctly using the %s, in most cases.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    7
    Quote Originally Posted by Adak View Post
    Correct you are!

    If the dest size is 3, then you need to copy just two chars, leave the for loop, and add
    Code:
    dest[i]='\0';
    Just after the for loop in tokenCopy(). NOW you have a string. Without that, you don't have a string - (unless C adds the end of string marker char for you). You have just a bunch of chars - not a string - and they won't print correctly using the %s, in most cases.
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newline and null terminator
    By dunsta in forum C Programming
    Replies: 2
    Last Post: 04-22-2010, 07:13 AM
  2. Terminator 4
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-23-2009, 11:15 PM
  3. Removing null terminator from a string
    By Canadian0469 in forum C Programming
    Replies: 3
    Last Post: 10-27-2007, 02:03 PM
  4. accept(ListenSocket, NULL, NULL); cause program to hang?
    By draggy in forum Networking/Device Communication
    Replies: 11
    Last Post: 06-16-2006, 03:40 PM
  5. Looking for null terminator
    By MiamiCuse in forum C Programming
    Replies: 3
    Last Post: 10-22-2005, 11:22 PM