Thread: how to define string without null at the end!?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    how to define string without null at the end!?

    Hi!
    I have a two string arrays, that I sent over the network, but the problem is that when I define them, GCC adds null at the end. I know it sopose to be like that, because it's string termination character, but I need to be able to define array, without it! Any suggestions? Thank you!

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    At the other end, just eliminate the null character.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Send only bytes of strlen().

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Quote Originally Posted by manutd
    At the other end, just eliminate the null character.
    my_arr[last_element] = null; ?

    Quote Originally Posted by Kennedy
    Send only bytes of strlen().
    I'll try that out right now!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it doesn't have a null character, it's not a string. A string by definition is zero or more characters terminated by a null character. Thus, if you omit the null character, you no longer have a string, you just have an array of characters.
    Code:
    char notastring[10] = { 'n', 'o', 't', 'a', 's', 't', 'r', 'i', 'n', 'g' };
    As such, you should not be using string based functions on it.


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

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    19
    Whatever you do with strings, the nice thing is they are still just arrays terminated with a null character, strlen allows you to know the size of the string and position of the null character, so you can handle it as either a string or an array of chars, in whatever way is convenient.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I have a two string arrays, that I sent over the network, but the problem is that when I define them, GCC adds null at the end
    So what's so hard about
    char buff[ ] = "hello";

    Yes there is a \0 at the end, but it's dead easy to ignore it by doing
    send( buff, strlen(buff) );
    rather than
    send( buff, sizeof(buff) );

    If you're receiving a \0, then the only reason is that you sent a \0.
    It's simply a matter of being able to count, and using the return result of various functions.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  2. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM