How do i append a null in string?!

This is a discussion on How do i append a null in string?! within the C Programming forums, part of the General Programming Boards category; I want to create a loop that adds the first value "this" by the command Code: strcat(l->value,l->db.param_value.val); now i want ...

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

    Question How do i append a null in string?!

    I want to create a loop that adds the first value "this" by the command

    Code:
    strcat(l->value,l->db.param_value.val);
    now i want to append a null and move it one more space to the right so i can have

    Code:
    "this"'\0'"is"'\0'
    if i do strcat continuously in a loop it just gives me "thisis", anyone have a suggestion on how to do this ?

    Ive tried the statement below it didn't work

    Code:
        l->value=  l->value[1 + strlen(l->db.param_value.val)]; AND
           l->db_cmd +=  strlen(l->db.param_value.val) + 1;
    Thank you!

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    The null char '\0' is not equivalent to the space char " ". You do realize this correct? The simplest thing to do is to add another strcat line and append whatever char you actually want to your string.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Well how would anything else know to stop at the second \0 and not the first?

    Creating it is easy enough, you just use memcpy() and do all the indexing and counting yourself.

    But all the str... functions will just stop at the first \0, regardless of whatever else you may do.
    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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you want a nul character in the middle of your string? You do know that the nul terminator effectively stops all string functions right there, right? That's what denotes the end of a string. strcat just picks up where the end of the string is (the first nul terminator it encounters) and adds the next string right there.

    You can't print out a string using any string functions that has embedded nul terminators and expect to get anything past the first nul.


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

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by KMAN999 View Post
    I want to create a loop that adds the first value "this" by the command

    Code:
    strcat(l->value,l->db.param_value.val);
    now i want to append a null and move it one more space to the right so i can have

    Code:
    "this"'\0'"is"'\0'
    if i do strcat continuously in a loop it just gives me "thisis", anyone have a suggestion on how to do this ?
    Assuming you actually want a space char, then you may want to do something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main (void) {
      
    	char result[30]={0};
    	char *string1 = "This";
    	char *string2 = "is a line";
    
    	strcat(result, string1);
    	printf("%s\n", result);
    	strcat(result, " ");
    	strcat(result, string2);
    	printf("%s\n", result);
    
    	getchar();
            return 0; 
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Presuming you really do want '\0' and not just a space for whatever reason:

    After a strcpy() or stcat() or similiar, there will be a '\0' at the value returned by strlen(). So an easy way to do this is with another pointer (and use strcpy, not strcat):

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    	int i;
    	char s[256]="this", *p = &s[strlen(s)+1];
    
    	for (i = 0; i < 5; i++) {
    		strcpy(p,"is");
    		p = &p[strlen(p)+1];
    	}
    
    // verify
    	for (i = 0; i < p-s; i++) {
    		printf("%d %c\n", s[i], s[i]);
    	}
    
    	return 0;
    }
    Output:
    116 t
    104 h
    105 i
    115 s
    0
    105 i
    115 s
    0
    105 i
    115 s
    0
    105 i
    115 s
    0
    105 i
    115 s
    0
    105 i
    115 s
    0

    The left column are ascii values. If you don't know what ASCII is, you should:

    http://en.wikipedia.org/wiki/ASCII

    Of course, '\0' (ascii 0) is not a printable character and you cannot output this whole array with a string function, because it is not a string; it's 6 strings. Note that you will get exactly the same sequence in memory by using an array of strings/2D char array.
    Last edited by MK27; 08-09-2011 at 03:07 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String append
    By SterlingM in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2009, 12:56 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. string::append
    By manzoor in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 08:39 AM
  4. append/copy to end of string
    By kristy in forum C Programming
    Replies: 10
    Last Post: 07-19-2003, 06:02 AM
  5. append val of int to a string
    By verryhi in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2002, 02:20 PM

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