null terminating a string

This is a discussion on null terminating a string within the C Programming forums, part of the General Programming Boards category; Hello, I am wondering if I declare a array of chars like this. Code: static char xml_buffer[1024]; I am clearing ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    null terminating a string

    Hello,

    I am wondering if I declare a array of chars like this.

    Code:
    static char xml_buffer[1024];
    I am clearing the array before I fill it by using one of the following. By setting the first char to zero:
    Code:
    xml_buffer[0] = 0;
    or is this preferred?
    Code:
    memset(xml_buffer, 0, sizeof(xml_buffer));
    However, the problem is when I fill the buffer I don't think the null is being inserted at the end of the string.

    I am using a xml parser that will fill the buffer.
    Code:
    success = xmlFillString(tree, xml_buffer, sizeof(xml_buffer), XML_NO_CALLBACK);
    I don't think the null is being automatically appended and the documentation doesn't specify either? Could be I am not clearing the buffer, or is there any way to do this manually?

    Many thanks,

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    You could always cast strlen(xml_buffer) and see how far your string actually reaches. If you are getting a lot of extra stuff, the data is probably not being null terminated.

    Do you have any documentation on xmlFillString? How does it work?

  3. #3
    UK2
    Join Date
    Sep 2003
    Posts
    112
    I have some documentation. But doesn't specify about null terminating.

    I guess this is something I have to do myself.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Well, would you mind posting it? Does it return how many bytes were written or anything? If that is the case then you can check the last byte or the very next one to see if it is null, and if not, you can set it yourself.

    If you want, you could write your own version of this function and use strcpy, which does append a null byte at the end.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Well having done the memset, try
    Code:
    success = xmlFillString(tree, xml_buffer, sizeof(xml_buffer)-1, XML_NO_CALLBACK);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Instead of using memset, another way to set all the bytes to 0 is like this:
    Code:
    static char xml_buffer[1024] = {0};
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21