Like Tree1Likes
  • 1 Post By CommonTater

trying to concatenate: help!

This is a discussion on trying to concatenate: help! within the C Programming forums, part of the General Programming Boards category; I am trying to construct a string message. It should start with a BEGIN-tag and end with en END-tag. I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    99

    trying to concatenate: help!

    I am trying to construct a string message. It should start with a BEGIN-tag and end with en END-tag. I am trying to concatenate the BEGIN-tag (declared as a #define) with a string and then with the END-tag.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BEGIN "@@"
    #define END "CCC"
    
    int main(void) {
    
    	char *beg;
    	char *end;
    	int i = 0;
    
    	beg = malloc(sizeof(BEGIN));
    	end = malloc(sizeof(END));
    	beg = strcpy(beg,BEGIN);
    	end = strcpy(end,END);
    	printf("----------------Begin of Round %i------------------\n",++i);
    	printf("beg pointer before concat: %s\n", beg);
    	printf("end pointer before concat: %s\n", end);
    	printf("BEGIN pointer before concat: %s\n", BEGIN);
    	printf("END pointer before concat: %s\n", END);
    	beg = strcat(beg, "Some beautiful message");
    //	beg = strcat(beg, end);
    	printf("beg pointer after concat: %s\n", beg);
    	printf("end pointer after concat: %s\n", end);
    	printf("BEGIN pointer after concat: %s\n", BEGIN);
    	printf("END pointer after concat: %s\n", END);
    	printf("----------------End of Round %i------------------\n\n",i);
    		
    	return 0;	
    }
    The output that I get is this:
    Code:
    ----------------Begin of Round 1------------------
    beg pointer before concat: @@
    end pointer before concat: CCC
    BEGIN pointer before concat: @@
    END pointer before concat: CCC
    beg pointer after concat: @@Some beautiful message
    end pointer after concat:  message
    BEGIN pointer after concat: @@
    END pointer after concat: CCC
    ----------------End of Round 1------------------
    (1) The main thing that I don't understand is the value of the end pointer after the concat call. Why is it changed to what it is.
    (2) Furthermore I do not really understand why I have to use the malloc statements in this case (without them the program crashed)
    (3) If you look at the code, there is one statement commented-out. If I run it, it crashes, why?

    Thanks for your efforts.
    Last edited by django; 08-07-2011 at 02:05 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm unclear on what you're trying to do here, but you have allocated a very small amount of memory, to try and hold the message string.

    MORE bytes, please!



    print out the sizeof your variables, and it will become much clearer to you.
    Last edited by Adak; 08-07-2011 at 01:52 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,278
    You are allocating a fixed amount of memory for "beg", but then adding more characters to it without reallocating more memory to hold them.

    You also aren't "free()"ing your allocated memory at the end of the program.

    You might also have better luck with:

    Code:
      beg = malloc(strlen(BEGIN)+1);  // plus one for null character
      end = malloc(strlen(END)+1);    // plus one for null character

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    I am trying to construct a string message. It should start with a BEGIN-tag and end with en END-tag. I am trying to concatenate the BEGIN-tag (declared as a #define) with a string and then with the END-tag.
    Like this....
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define BEGIN "@@"
    #define END "CCC"
    
    int main (void)
      {  char *result = calloc(100,sizeof(char));  // create some string space
    
        strcat(result, BEGIN);                       // result holds @@
        strcat(result, "this is a message");     //  result holds @@this is a message
        strcat(result, END);                         // result holds @@this is a messageCCC
    
        printf("%s\n\n", result);
        free(result); 
        return 0; }
    Alll so nice and simple...
    Last edited by CommonTater; 08-07-2011 at 03:44 PM.
    django likes this.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Tater thanks, way more simple and elegant. Still you would help my understanding if you could answer (1) from the start of this post. Then I'll be ready to abandon this wreck...

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    I tested a bit more. The end pointer seems to be overwritten because I allocate a string to the beg pointer which is way longer than the space allocated. If I augment the malloc of beg, the end pointer gives the value I would expect. Does this mean that if in C you load more into a pointer segment than you allocated, it can be screwing up other pointers and so your whole program?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't seem to understand concatenating.

    You have two words: "Bob" and "Bobberson". You want them to be one word. But the space for the first word just holds "Bob", plus the nul character at the end. You have to make more space so that you can stick them together.

    length of "Bob" + one space between them + length of "Bobberson" + 1 nul character at the end = size of new space.

    Make that space. Copy "Bob" into it. Stick a space on the end. Now stick "Bobberson" onto the end. There, you have successfully concatenated "Bob" " " "Bobberson" into the same chunk of memory.

    Or, if the space for "Bob" was big enough for both words in the first place, you can skip the step of making new space. Just copy space + "Bobberson" onto the end. In fact, that is what strcat(s,append) assumes: s has enough space for append + a nul character.

    If you have made new space for it, and have copied both words there, you still have your old words "Bob" and "Bobberson" floating around, unless you used realloc on "Bob" (assuming that you actually could - ie: it's not a string literal). You are just making new space, or resizing the current space, and then copying what was in some old space into that new space.

    It has nothing to do with how many pointers you make. You don't concatenate by doing:
    Code:
    ptr1 = "Bob"
    ptr2 = "Bobberson"
    ptr1 = ptr1 + ptr2;
    If that is what you are trying to say in the previous post.


    Quzah.
    Last edited by quzah; 08-07-2011 at 04:49 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    No, that's not what I am saying. I simply would like an answer to question (1) of my original post.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by django View Post
    No, that's not what I am saying. I simply would like an answer to question (1) of my original post.
    It doesn't matter what it is, because you are invoking undefined behavior:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BEGIN "@@"
    #define END "CCC"
    
    int main(void) {
    
    	char *beg;
    	char *end;
    	int i = 0;
    
    	beg = malloc(sizeof(BEGIN)); /* 3 characters worth of space */
    	end = malloc(sizeof(END));  /* 3 characters worth of space */
    	beg = strcpy(beg,BEGIN);
    	end = strcpy(end,END);
    	printf("----------------Begin of Round %i------------------\n",++i);
    	printf("beg pointer before concat: %s\n", beg);
    	printf("end pointer before concat: %s\n", end);
    	printf("BEGIN pointer before concat: %s\n", BEGIN); /* not a pointer, a string literal */
    	printf("END pointer before concat: %s\n", END); /* not a pointer, a string literal */
    	beg = strcat(beg, "Some beautiful message"); /* you don't have space to do that */
            /* nothing here matters, because you just trashed
                whatever memory was beyond the space
                allocated for 'beg' */
    Running off the end of your allocated space is undefined behavior. The fact that your program actually runs at this point is pure luck. Any values you have anywhere are irrelevant at this point.


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

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Ok, thanks. I am leaving this thread, my initial question was too ill-formed anyway. Thanks for the comments.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    I tested a bit more. The end pointer seems to be overwritten because I allocate a string to the beg pointer which is way longer than the space allocated. If I augment the malloc of beg, the end pointer gives the value I would expect. Does this mean that if in C you load more into a pointer segment than you allocated, it can be screwing up other pointers and so your whole program?
    That's correct, you only allocate sizeof(pointer) for your string space. Most likely, being consecutive calls, the memory exists tail to nose and when you write your message you are overwriting the memory for your end string.

    In any case... no matter what's happening there... THAT is not the way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using ## to concatenate tokens
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-25-2011, 10:36 PM
  2. concatenate hex numbers
    By ryanE in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2010, 12:00 PM
  3. concatenate
    By violatro in forum C Programming
    Replies: 16
    Last Post: 06-04-2010, 09:22 AM
  4. Concatenate string and int
    By millsy5 in forum C Programming
    Replies: 1
    Last Post: 01-28-2010, 03:43 AM
  5. concatenate
    By port in forum C Programming
    Replies: 1
    Last Post: 11-25-2002, 08:53 PM

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