Thread: char string copy

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    char string copy

    Hi..

    Heres an example of a code:

    Code:
    char data[128] = "here is some text";
    char info[128] = " and another text..";
    char *p1, p2;
    
    p1 = data;
    p1 += 5;
    now i want to copy info to p1 so I would get string with "is some text and another text..";

    What would be the proper way to do that?

    Thanks a lot for your help guys

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    the =+ works with strings, ie:

    string One = "This is a ";
    string Two = "Complete sentence!!";
    string Three = One + Two;
    One += Two;

    etc

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Use strcpy and strcat -- but you'll need to allocate some memory to p1 to be able to write to it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    So I could do:

    Code:
    char data[128] = "here is some text";
    char info[128] = " and another text..";
    char *p1, p2;
    
    p1 = data;
    p2 = info;
    
    p1 += p2;
    and i would get printf(p1) : "here is some text and another text.." ?


    What way would you prefer:

    Code:
    sprintf(tmp, "%s%s", p1, p2);
    or

    Code:
    strcpy(tmp, p1);
    strcat(tmp, p2);
    Whats more professional?
    Last edited by l2u; 05-19-2006 at 09:00 AM.

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>What would be the proper way to do that?<<

    Use std::string, not char arrays as twomers has already described.

    >>printf<<

    No, that would be std::cout. This is the c++ board, char arrays and printf are for c programming.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    std::string data = "here is some text";
    std::string info = " and another text..";
    
    data+=info;
    
    std::cout<<data<<std::endl;
    }
    Last edited by Ken Fitlike; 05-19-2006 at 12:11 PM. Reason: typo
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    If you wanted to do it in C, you could
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <malloc.h>
    
    int main()
    {
    	char data[128] = "here is some text";
    	char info[128] = " and another text..";
    	char *p1, p2;
    
    	p1 = (char*) malloc((strlen(data) + strlen(info) + 1) * sizeof(char));
    	strcpy(p1, data);
    	strcat(p1, info);
    
    	printf("%s\n", p1);
    	free(p1);
    	return 0;
    }
    I don't know what you wanted to do with p2, but if you wanted to define two pointers, you should have
    Code:
    char *p1, *p2;
    Your code would have defined one character pointer and one character.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you're going to do it in C, there is a perfectly good function for something like this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
       char data[] ="Here is some text ";
       char info[] = "which is a part of a bigger sentence.";
    
       /*  I cached the length of both strings to reduce some overhead */
       const int DATALEN = strlen(data);
       const int INFOLEN = strlen(info);
       char *sentence = malloc(DATALEN + INFOLEN + 1);
    
       sprintf(sentence, "%*s%*s", DATALEN, data, INFOLEN, info);
       puts(sentence);
    
       free(sentence);
    
       return 0;
    }
    Last edited by whiteflags; 05-19-2006 at 11:11 AM.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Why is * sizeof(char) needed?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by l2u
    Why is * sizeof(char) needed?
    It's not. It is the same as multiplying by one.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM