Results 1 to 15 of 15

Thread: Append Array with Pointer

  1. #1

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    Append Array with Pointer

    Hello,
    I have a question. I am trying to code a function that will append two array.

    Here is the code, all work but the value doesn't return well, why?
    Code:
    #include <iostream>
    using namespace std;
    
    void CopyChaineSuite(char *&s1, char *s2);
    
    void main()
    {
    	char cPrenom1[80] = "Me";
    	char cPrenom2[80] = "andYou";
    	CopyChaineSuite(cPrenom1, cPrenom2);
    	cout << "Prenom #1 : " << cPrenom1 << endl;
    	cout << "Prenom #2 : " << cPrenom2 << endl;
    }//Fin du main
    
    void CopyChaineSuite(char *&s1, char *s2)
    {
    	
    	char *ptrDebut = new char;
    	char *p = ptrDebut;
    
    	while(*s1 != '\0')
    		*ptrDebut++ = *s1++;
    
    	while(*s2 != '\0')
    		*ptrDebut++ = *s2++;
    	*ptrDebut = '\0';
    
    	s1 = p;
    }//Fin de CopyChaineSuite

  2. #2

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    I'm happy to found that is not a easy problem....

  3. #3
    New Member
    Join Date
    Nov 2002
    Location
    Petawawa Ontario
    Posts
    5
    try this:

    Code:
    main() {
    char *firstString, *secondString;
    firstString = (char *)malloc( 80 ); //or however many bytes you want
    secondString = (char *)malloc( 80);
    
    firstString = "something";
    secondString = " else";
    
    strcat( firstString, secondString );
    
    
    /***********
    or even
    strncat( firstString, secondString, max_bytes_to_append );
    **********/
    
    //and then
    
    free( firstString );
    free( secondString );
    
    //when you are done with them....

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    main() {
    int main(void) {

    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    The idea with my question was how to make it with pointer without any stuff already done like strcat.

  6. #6
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    How about -
    Code:
    char *cat(char *dest,char *src){
          register char *buf,*s;
          for(buf=dest;*buf;buf++);  /* goto end of current array */
          for(s=src;*s;buf++,s++) *buf=*s; /* add characters */
          *buf=0x00; /* null terminate string */
          return dest;

  7. #7

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    Why register???

  8. #8

    Thread Starter
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362

    ok

    Code:
    void CopyChaineSuite(char *s1, char *s2)
    {
    	while(*s1 != '\0'){s1++;}
    	while(*s2 != '\0'){*s1++ = *s2++;}
    	*s1 = '\0';
    }//Fin de CopyChaineSuite
    This is my code and yours together and it work well.

    I have an other question, can I know if it's possible to do s1--... if I want to make a reverser function I will need it right?

  9. #9
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Yes you can go backwards with a pointer. One caveat -
    Don't go back before the start of the buffer or you will have some nasty surprises Try something like ptrdiff() below:

    Code:
    char *strrev(char *s){ // reverse a string
         register char *buf,*dest;
         char *tmp;
         if(!*s) return s;
         for(buf=s;*buf;buf++);     
         tmp=calloc(1,ptrdiff(buf,s);
         buf--;
         for(dest=tmp; ptrdiff(buf,s);buf--,dest++) *dest=*buf;
         *dest=0x00;
         cpy(s,tmp);
         return s;
    }
    void cpy(char *dest,char *src){
          register char *buf, *s;
          for(s=src,buf=dest;*s;buf++,s++)*buf=*s;
          buf=0x00;
          return;
    }
    int ptrdif(void *a, void *b){
            return (int)a-(int)b;
    }

  10. #10
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    You can't change what an array that is declared as "char name[size];" points to. I suggest that you use C++ strings:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void CopyChaineSuite(char *&s1, char *s2);
    
    int main()
    {
    	string cPrenom1 = "Me";
    	string cPrenom2 = "andYou";
    	CopyChaineSuite(cPrenom1, cPrenom2);
    	cout << "Prenom #1 : " << cPrenom1 << endl;
    	cout << "Prenom #2 : " << cPrenom2 << endl;
            return 0;
    }//Fin du main
    
    void CopyChaineSuite(string &s1, const string& s2)
    {
            s1 += s2;
    }//Fin de CopyChaineSuite

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In that case, what's the point of another function call?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    habit of using functions?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Quiet, you

    How's Sq?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    hehe

    the compiler is still being planned, the language definitions are quite set now i think.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    char *ptrDebut = new char;
    Once I'm away everyone goes blind...


    A single character is not enough to hold two complete arrays.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width