Thread: Help on another pointer program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    19

    Help on another pointer program

    Once again, I need a push in the right direction.
    The problem is to make a pointer program that copies one string onto the end of the other. I did this and it works perfectly. However, the 2nd part of the problem says if the first string doesn't have enough memory, to add what it can to the first string and null terminate it. Here's the program I wrote so far:

    Code:
    #include <stdio.h>
    
    //This adds one string to the end of another
    //Input: You need two pointers to two different strings
    //Output: It doesn't return anything. However, it does add one string to the other
    void _strcat(char * s_pointer, char * t_pointer)
    {
        int i = 0;    //my two element counters
        int j = 0;
    
        while (*(s_pointer + i) != '\0') //finds the end of s
        {
            i++;
        }
        
        while ((*(s_pointer + i) = *(t_pointer + j)) != '\0') //copies t to s
        {
            i++;
            j++;
        }
        return; 
    }
    
    int main()
    {
        char s[15] = "Four score";
        char t[] = " and seven years ago";
        char * s_pointer = &s;
        char * t_pointer = &t;
        _strcat(s_pointer, t_pointer); //using my function
        printf("%s\n", s);        //output
        return 0;
    }
    I figured the memory part will go in the 2nd while loop? But I don't know how to add extra memory. Right now, the program will crash. I'm using Four score and seven years ago as a test. I cannot simply put a bigger number in the s array index. Thanks for the help

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by KrazySocoKid View Post
    But I don't know how to add extra memory.
    It doesn't ask you to add extra memory, it says to append what you can and terminate. But for that, you'll need an integer which will hold the maximum size of the string.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You are trying to stuff 31 characters into an array that has a size of 15. It "may" appear to work but you are in fact writing to memory that does not belong to the array. You the programmer must insure that the destination string is large enough to hold the combined string. Instead of just trying to write a replacement for the standard strcat() function you may want to consider adding another parameter to your function, the size of your destination array. Then check to insure that you do not access this array out of bounds. You can not easily "Add" memory to a statically allocated array. If you want your function to "grow" your destination string to allow for a larger size you should be using a dynamically allocated array for your destination string.

    I don't see why you are creating the pointers you could just use:
    Code:
    _strcat(s, t);
    Jim

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Ok I created an integer
    Code:
    int k = i +j;
    I put it after the 2nd while loop. But I still don't know how to add it to the string.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Show your current code. Your question does not make any sense what are you trying to add to the string? The integer? What?

    Jim

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Here is my current code
    Code:
    #include <stdio.h>
    
    //This adds one string to the end of another
    //Input: You need two pointers to two different strings
    //Output: It doesn't return anything. However, it does add one string to the other
    void _strcat(char * s_pointer, char * t_pointer)
    {
        int i = 0;    //my two element counters
        int j = 0;
    
        while (*(s_pointer + i) != '\0') //finds the end of s
        {
            i++;
        }
        
        while ((*(s_pointer + i) = *(t_pointer + j)) != '\0') //copies t to s
        {
            i++;
            j++;
        }
        int k = i + j;
        return; 
    }
    
    int main()
    {
        char s[15] = "Four score";
        char t[] = " and seven years ago";
        char * s_pointer = &s;
        char * t_pointer = &t;
        _strcat(s_pointer, t_pointer); //using my function
        printf("%s\n", s);        //output
        return 0;
    }
    I'm following GReaper's advice. However I have no Idea where to use the integer

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In line 27... make your buffer bigger... try char s[100]; ... there's no need to skimp and the integer is unnecessary.

    This is one of the inherrent problems with C ... it's real easy to overflow a buffer and wreck the program's stack and, without getting into really complex schemes with malloc() and realoc() there's not much you can do about it.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No to follow GReapers's advice you will need to add a parameter to your function that holds the size of the destination string.
    Code:
    void _strcat(char * s_pointer, char * t_pointer, int size)
    {
    
    }
    int main(void)
    {
     /*......*/
       char dest[10];
    /*....*/
      _strcat(dest, src, sizeof(dest));
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on simple pointer program
    By KrazySocoKid in forum C Programming
    Replies: 9
    Last Post: 10-21-2011, 10:24 AM
  2. Need help with this error in pointer program
    By camel-man in forum C Programming
    Replies: 20
    Last Post: 09-09-2011, 10:51 PM
  3. Simple Pointer Program
    By eater in forum C Programming
    Replies: 3
    Last Post: 05-28-2009, 11:35 AM
  4. Help with a pointer program
    By steve568 in forum C Programming
    Replies: 1
    Last Post: 11-14-2008, 06:51 AM
  5. Why Do i get NULL pointer assignment In this program?
    By chottachatri in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2008, 05:18 AM