Thread: How to iterate through char array with multiple null terminators?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    How to iterate through char array with multiple null terminators?

    Hi, I would really appreciate if anyone could help me on the following problem...


    A char array has multiple strings, each of which ends with a null terminator '\0'...

    For example
    Code:
    char A={string1'\0'string2'\0'string3.....}
    So if I do print char array, or some other string manipulations, it's always gonna stop at the very first occurrence of '\0', and ignores the rest of the chars, is there a way to get around with it?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Write your own string functions.

    You'll have to have some way of figuring out where the end is; say, a count of substrings, or the length of the array in bytes, etc.

    If you want to treat this as multiple strings, you can get yourself a pointer to the beginning of each substring (again, somehow knowing where the last string is), and use C's string manipulation functions. But if you want to treat the whole array as a string-like object, you have to do something else. At the very least memcpy() might be handy for copying your string-like thing.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There are certain functions I am aware of (the windows GetOpenFileName function is one such example) that pass arguments consisting of such character arrays. They differentiate the end of the data by a double null terminator. You'd basically need code that checks for a double null at the end of your data if you adopted that route.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dligentSlacker View Post
    Hi, I would really appreciate if anyone could help me on the following problem...


    A char array has multiple strings, each of which ends with a null terminator '\0'...

    For example
    Code:
    char A={string1'\0'string2'\0'string3.....}
    So if I do print char array, or some other string manipulations, it's always gonna stop at the very first occurrence of '\0', and ignores the rest of the chars, is there a way to get around with it?
    You need to set pointers to the beginning of each string in the chain...
    Code:
    char multistring[1024] = "This\0is\0my\0multi-string\0\0";
    char *idx = multistring;
    
    while (*idx)
      { printf("%s", idx);
         idx += strlen(idx) + 1; }

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The trouble with using a double \0 to denote the end of the multistring is that you cannot then have legitimate empty strings.

    If you know you might have empty strings and that there are characters in your character set that will never appear in a legitimate string you might use one of those as your end-of-multistring delimiter.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then you need to start passing your buffer's size around every time you use it. You can't expect to use normal string functions on not-normal-strings.


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

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TheBigH View Post
    The trouble with using a double \0 to denote the end of the multistring is that you cannot then have legitimate empty strings.

    If you know you might have empty strings and that there are characters in your character set that will never appear in a legitimate string you might use one of those as your end-of-multistring delimiter.
    Unfortunately Windows convention disagrees with you... Many Windows functions return multi-strings. They send C strings, null separated with a double null at the end. And, no they cannot contain empty strings.

    The GetOpenFilename() function is one such example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new functions in a win32 application
    By xniinja in forum Windows Programming
    Replies: 30
    Last Post: 07-30-2010, 09:55 PM
  2. I need help :(
    By ramenen in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2010, 04:31 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM