Thread: Null terminating a character pointer?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Null terminating a character pointer?

    Hello, I have a void pointer 'data' which I used to cast to a string later on by a method that sends in another data

    Im using

    Code:
    strncpy((char*)this->data,data,len);
    How do I make sure that this->((char*)data) is null terminated?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    if(data[strlen(data)] == '\0')
    		cout<<"yes"<<endl;
    edit: Nope, won't work. strlen() only works on null terminated strings.
    Last edited by 7stud; 11-28-2005 at 08:22 AM.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    What I meant was, how do I null terminate it?

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    strcpy((char*)data,'\0'); ?

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Isnt that deprecated?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    strcpy() only works with null terminated strings, too--that's how strcpy finds the end of the first string.

    I don't think you can unless you know the size of the array. With any C++ array, you have to pass the size around with it--otherwise you'll go out of bounds.
    Last edited by 7stud; 11-28-2005 at 08:29 AM.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    So what do I do when my assignment says

    IOLabel &operator=(const char *data); copies the maximum of "len" character from the incoming argument "data" into the data attribute of the class. Make sure that the data is null terminated.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    strncpy's destination string will be 0-terminated as long as the source-string is shorter then the specified nr of characters.
    To be shure that it is alwais 0-terminated you could just
    Code:
    ((char*)this->data)[len-1]='\0';
    Kurt

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    It's ambiguous, but this sentence:
    Make sure that the data is null terminated.
    is probably referring to the data member of the class. Typically, you'll use 'new' to create the memory for your member variable to store len + 1 characters. Then you copy each character from the source string into your member variable string, and finally for the last index position of your member variable string, you assign it a '\0' character.
    Last edited by 7stud; 11-28-2005 at 08:45 AM.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by INFERNO2K
    strcpy((char*)data,'\0'); ?
    Aside from the syntax error (strcpy expects two pointers to characters, not a pointer and a char) this will cause the first character of data to be the null character, thus emptying the string. The syntax error can be fixed by doing:
    Code:
    strcpy((char*)data, "");
    Quote Originally Posted by 7stud
    strcpy() only works with null terminated strings, too--that's how strcpy finds the end of the first string.
    It only matters that the source is null terminated. You are probably thinking of strcat() which appends the source onto the destination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  2. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM