0

This is a hw assignment that I am having a lot of difficulty with. I'm tasked with creating my own function that accepts two strings and will concatenate the two strings together and will return a character pointer to the new string. This is what I currently have:

char * my_strcat(char p[], char t[]) {
    printf("Made it into my_strcat.\n");

    int size1;
    int size2;

    size1 = my_strlen(p);
    size2 = my_strlen(t);

    int size3;
    size3 = size1 + size2 +1;

    printf("This many characters allocated for in memory: %i\n", size3);

    char* MemLoc = (char *)malloc(sizeof(char)*(size1+size2+1));
    char* BookMark = MemLoc;

    printf("Address of MemLoc: %p\n", MemLoc);
    printf("Address of BookMark: %p\n", BookMark);

    int count = 0;

    while(count < size1) {
        *BookMark = p[count];
        printf("Address of MemLoc: %p\n", MemLoc);
        printf("Latest char: %c\n", *MemLoc);
        count++;
        BookMark++;
        printf("Address of MemLoc: %p\n", MemLoc);
    }

    count = 0;

    while(count < size2) {
        *BookMark = t[count];
        printf("Latest char: %c\n", *MemLoc);
        count++;
        BookMark++;
    }

    *BookMark = '\0';
    printf("Concatenated string: %s\n", MemLoc);

    return MemLoc;
}

I have a lot of print statements in there trying to determine my error but I still can't pin it down. Depending on what type I print as, I am getting "nil", "null", and nothing for the last print statement. The last print statement (in my opinion) should be printing the final concatenated string.

Also, I had to construct the my_strlen method to calculate string length. This is working correctly and returns the correct length values of the strings sent in. Any suggestions for fixing this method?

18
  • 2
    Why the +4 ? I think it should be BookMark++ Commented Apr 27, 2014 at 15:15
  • 1
    The size of a char is 1. The size of a char* is 4 (depending on your architecture). And you always print MemLoc which is not changing. Commented Apr 27, 2014 at 15:20
  • 2
    Whe loop for while count < size1 - 1? As you use strlen to get size1 and size2 that length already is without the terminator. Either do count < size1 (most usual) or size <= size - 1. Commented Apr 27, 2014 at 15:23
  • 2
    Also, in C you should not cast the return of malloc. Commented Apr 27, 2014 at 15:24
  • 2
    You should consider turning up your compiler's warning level. With GCC and Clang, it is -Wall that will help. Then you just need to understand what the compiler tells you. It is usually very helpful. Commented Apr 27, 2014 at 15:28

1 Answer 1

1

You might use a printf of the form

    printf("built string is '%.*s'\n",size3,MemLoc);

or

    printf("build string is '%.10s'\n",MemLoc);

to help you debug. By using the precision limit on the %s format specifier you can prevent the format conversion from running off of the end of the unterminated partially built string. Since its just debug and you probably know the length of your test case there is really no harm in using the fixed length version.

For help in this debugging you would probably also want to

    memset(MemLoc,'#',size3);

after you malloc it and before you start construction. This can help avoid confusion with garbage that was already in memory vs. what you are doing to it.

With that initialization and scattering the prints around, I'm hopeful you will be able to debug your problems.

I agree with friz's comment that the +4 for stepping through the string doesn't make sense.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I solved the error(s). Apparently I "can't answer my own question" for another 8 hours. I will be sure to post my solution then. Thanks for the help everyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.