Thread: concatenating a string with nul's in it

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    concatenating a string with nul's in it

    how do you concatenate a string with 0 bytes ('\0') in them? i tried to write a while loop but it doesn't work either. i don't know maybe i just did something wrong. here is my code:

    Code:
    void stradd(char str1[], char str2[], int a, int offset)
    {
    	int i = 0;
    
    	while (i < a)
    	{
    		str1[offset + i] = str2[i];
    		i++;
    	}
    }
    i set str1 to the char array i want to add to, str2 to the string i want to add, a to the amount of characters i want to add, and offset to the position in str1 i want to start adding str2 at.

    is there anything wrong with this code or am i just somehow doing something wrong?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I don't know what your "wrong" is but I'm assuming that something is going wrong when you're trying to use the string in combination with other, standard functions like printf or the ones from string.h.
    This would be because after the concatenation str1 is not ended with a '\0' so you'd have to put a str1[offset + i] = '\0'; after the loop.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    i'm wondering if its because this function is in a dll and its being called by a program that expects a null terminated string i think. the only thing is the program calling it can add a 0 byte to the string and that will show... do you think thats the problem?

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Still not very clear to me what result you're expecting.
    Give me an example of str1, str2 and the result.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There is certainly a problem if offset+i exceeds the range of str2 or if i exceeds the range of str1.
    "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

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    str1 would be "hey i'm going to the store" and str2 would be "\0\0\0 ok". i want to turn that into "hey i'm going to the store\0\0\0 ok".

    when i'm doing it in my program though the array starts empty and i try to add an array that starts with a \0 immediatly.

  7. #7
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Well, your function works as it should so the problem lies elsewhere.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > how do you concatenate a string with 0 bytes ('\0') in them?
    You don't, since they're no longer strings.

    You now have to use the mem... functions rather than the str... functions, and keep a separate count of how many bytes you have stored.

    > i want to turn that into "hey i'm going to the store\0\0\0 ok".
    And then what?
    Sure you can create it, but what are you going to do with it when you've got it. The "ok" is going to be invisible to every standard string function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concatenating integer to string
    By RyanC in forum C++ Programming
    Replies: 8
    Last Post: 05-24-2019, 04:12 AM
  2. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  3. concatenating a string
    By simpatico_qa in forum C Programming
    Replies: 8
    Last Post: 04-08-2009, 04:06 PM
  4. concatenating two strings
    By js_badboy in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 08:27 PM
  5. Concatenating: characters->string->vector (string) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2002, 01:14 PM