简体   繁体   English

在C中创建字符串连接功能

[英]Creating string concatenation function in C

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. 根据我的打印类型,我得到的是“ nil”,“ null”,最后一个打印语句什么也没有。 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. 另外,我必须构造my_strlen方法来计算字符串长度。 This is working correctly and returns the correct length values of the strings sent in. Any suggestions for fixing this method? 这可以正常工作,并返回发送的字符串的正确长度值。对解决此方法有何建议?

You might use a printf of the form 您可以使用以下形式的printf

    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. 通过对%s格式说明符使用精度限制,可以防止格式转换从未终止的部分构建的字符串的末尾开始。 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. 我同意friz的意见,即逐步输入字符串的+4没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM