concatenating a string with '

This is a discussion on concatenating a string with '

Thread: concatenating a string with '\0's in it

's in it
within the C++ Programming forums, part of the General Programming Boards category; how do you concatenate a string with 0 bytes ('

Thread: concatenating a string with '\0's in it

') in them? i tried to write a while loop but ...

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

    concatenating a string with '\0'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
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    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
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    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,803
    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
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Well, your function works as it should so the problem lies elsewhere.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > 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. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM