appending a string onto end of another string

This is a discussion on appending a string onto end of another string within the C Programming forums, part of the General Programming Boards category; I have been reviewing this stuff on strings and I am confused. Function needs to append contents of string s2 ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    appending a string onto end of another string

    I have been reviewing this stuff on strings and I am confused.

    Function needs to append contents of string s2 to the end of string 1 and return a pointer to resulting string.

    Would the following work using the function declaration as shown???
    Code:
    char * catstr(char * s1, const char * s2)
    {
         char *pc;
         pc = s1;
        
         while (*pc != '\0')
             pc++;
    
         while (*s2 != '\0')
             {
                  *pc = *s1;
                  pc++;
                  s1++;
              }
    
           *pc = '\0';
           
           return s1;
    }
    I am still confused with functions with pointers as function types and as function parameters (arguments).

    This is for review for my final. It's been a while since working with simplified code like this. I feel I am either missing something or making this more complicated than it should be.

    Also, if not using strcat or strcpy commands, is there other ways to append strings to other strings???
    Sue B.

    dazed and confused


  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    EDIT: double post
    Last edited by QuestionC; 12-09-2001 at 09:47 PM.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    It all looks good except
    Code:
         while (*s2 != '\0')
             {
                  *pc = *s1;
                  pc++;
                  s1++;
              }
    Should be
    Code:
         while (*s2 != '\0')
             {
                  *pc = *s2;
                  pc++;
                  s2++;
              }
    And, while the details of this function may change, the basic idea isn't going to change. This is pretty much 'the' way to concatenate strings. Any other way to do it is really gonna be the same way.

    There are two important features about this function that you need to note. First, off, this function changes s1, and second, it assumes that there is enough memory allocated to s1. These still make it a useful function, but to make it work otherwise, you would have to do this...
    Code:
    // const stops us from changing the strings
    char * catstr(const char * s1, const char * s2)
    {
         char *pc, *result;
    
         result = pc = malloc (strlen(s1) + strlen(s2) + 1);
         // I know, we aren't supposed to use string functions, but I
         //  think this one is okay.
         // We have to use two pointers so we have one to walk the 
         //  string, and one to stay at the beginning.  This becomes
         //  important when we have to return something.
        
         // This loop puts the values of s1 into pc.
         while (s1 != '\0')
          {
             *pc = *s1;
             pc++
             s1++;
          }
    
         // This loop puts the values of s2 into pc
         while (*s2 != '\0')
             {
                  *pc = *s2;
                  pc++;
                  s2++;
              }
    
           // Terminate the string.
           *pc = '\0';
           
           // pc now points at the end of the string, so we have to use
           //  result, which still points at the beginning of the string.
           return result;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    What's a good main() function to test this function ??

    I tried the following but am getting a type mismatch and such.
    Please explain what compilation error message means.

    Code:
    
    #include <stdio.h>
    
    
    main()
    {
      char str1;
      char str2;
      char s1, s2;
    
      printf("Input a string of characters");
      s1 =  getchar();
      printf("Input another string, it will be added to the 1st");
      s2 = getchar();
    
      catstr(s1, s2);
    
      printf("Here is your str1 string: ");
      putchar(str1);
      printf("\n Here is your str2 string : ");
      printf("\n");
      putchar (str2);
    
      return 0;
    }

    ERROR message:


    17 /accounts/student2/srbkpk/cs225/final gcc cat.c
    cat.c:27: warning: type mismatch with previous implicit declaration
    cat.c:15: warning: previous implicit declaration of `catstr'
    cat.c:27: warning: `catstr' was previously implicitly declared to return `int'
    Sue B.

    dazed and confused


  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    17 /accounts/student2/srbkpk/cs225/final gcc cat.c
    cat.c:27: warning: type mismatch with previous implicit declaration
    cat.c:15: warning: previous implicit declaration of `catstr'
    cat.c:27: warning: `catstr' was previously implicitly declared to return `int'
    The problem is that you have not declared the function 'catstr' before using it. Since this is the case, it assumes that it returns an integer. When it actually gets to the function itself, it finds that the function is set to return a character pointer. Thus, it gives you the errors.

    To fix this, tell the compiler what the function does before hand. You can do this by:

    1) declaring it inside or before main:

    char *catstr( char *str1, const char *str2 );

    2) writing the whole function before 'main' so that 'main' is the last function in your program.

    Either one will do the trick.

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

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    After you declare your prototype, you'll also have to get strings instead of chars...
    Code:
    #include <stdio.h>
    
    
    main()
    {
      char str1;
      char str2;
      char s1 [80], s2[80], s3[80];
    
      printf("Input a string of characters");
      scanf ("%s", s1);
      printf("Input another string, it will be added to the 1st");
      scanf ("%s", s2);
    
      s3 = catstr(s1, s2);
    
      printf("Here is your str1 string: ");
      printf ("%s", s1);
      printf("\n Here is your str2 string : ");
      printf("%s", s2);
      printf("\n Here is your str3 string : ");
      printf("%s", s3);
      printf ("\n");
    
      return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    this code is giving a compilation error; I don't know what to change on the red highlighted line.



    Code:
    #include <stdio.h>
    
    char * catstr(char * s1, const char * s2);
    
    main()
    {
      char s1[80], s2[80], s3[80];
    
      printf("Input a string of characters");
      scanf("%s",s1);
      printf("Input another string, it will be added to the 1st");
      scanf("%s",s2);
    
      s3 =  catstr(s1, s2);
    
      printf("Here is your s1 string: %s\n",s1);
      printf("Here is your s2 string : %s\n",s2);
       printf("Here is your s3 string : %s\n",s3);
      printf("\n");
    
      return 0;
    }
    
    char * catstr(char * s1, const char * s2)
    {
         char *pc;
         pc = s1;
    
         while (*pc != '\0')
             pc++;
    
         while (*s2 != '\0')
             {
                  *pc = *s1;
                  pc++;
                  s1++;
              }
    
           *pc = '\0';
    
           return s1;
    }
    here's the compilation error I am getting:


    cat.c: In function `main':
    cat.c:14: incompatible types in assignment


    line 14 is the red highlighted line

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    s3 is an array. You cannot assign to it in this manner. This is incorrect. If s3 were a character pointer, your usage would be correct. Perhaps you should instead assign the return value to a character pointer first, then use strncpy into s3?

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

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Oh, my bad... change this:
    char s1[80], s2[80], s3[80];

    to this...
    char s1[80], s2[80], *s3;

    can't change the value of an array.
    Callou collei we'll code the way
    Of prime numbers and pings!

  10. #10
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Still getting a Seg Fault
    and are we sure the catstr function is okay.
    What causes a Seg Fault anyways??

    Code:
    #include <stdio.h>
    
    char * catstr(char * s1, const char * s2);
    
    main()
    {
      char s1[80], s2[80], *s3;   /*  changed to a pointer */
    
      printf("Input a string of characters");
      scanf("%s",s1);
      printf("Input another string, it will be added to the 1st");
      scanf("%s",s2);
    
      s3 =  catstr(s1, s2);          /* is this call okay ?? */
    
      printf("Here is your s1 string: %s\n",s1);
      printf("Here is your s2 string : %s\n",s2);
       printf("Here is your s3 string : %s\n",s3);
      printf("\n");
    
      return 0;
    }
    
    char * catstr(char * s1, const char * s2)
    {
         char *pc;
         pc = s1;
    
         while (*pc != '\0')
             pc++;
    
         while (*s2 != '\0')
             {
                  *pc = *s1;
                  pc++;
                  s1++;
              }
    
           *pc = '\0';
    
           return s1;
    }
    Sue B.

    dazed and confused


  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    A seg fault means that you are writing to, or reading from, an illegal memory address. 90% of the time, this means you're trying to use a pointer that is pointing to NULL.

    The rest of the time, it's because you're doing something like this...
    Code:
    while (*s2 != '\0')
             {
                  *pc = *s1;
                  pc++;
                  s1++;
              }
    Now, the value of s2 never actually changes in this loop, so *s2 is going to always be the first character of the second string. So, unless it is an empty string, *s2 != '\0' will always be true. Therefore, the pointers, s1 and pc will continue to increment all the way into the nether-regeions of your computer's memory, untill they reach the point where your line accessing that memory (*pc = *s1) ends up trying to access an illegal memory location, and viola, segmentation fault.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  3. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 03:40 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

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