0

有人可以帮我处理这段代码。我需要将这两个指针附加在一起,但它对我不起作用。该代码不会将指针添加在一起。我认为 *mystrcat 函数是错误的。

// stringAdds.cpp : Defines the entry point for the console application.
//

char *mystrcat(char *s, char *p);

int _tmain(int argc, _TCHAR* argv[])
{
    char myChar = 0;
    int i = 0;

    char *s = (char*) malloc (1);
    char *p = (char*) malloc (1);

    printf("Input s: ");
    while ((myChar=getchar()) != '\n')
    s[i++]=myChar;
    s[i]='\0';
    //scanf("%s", &s);
    printf_s("%s", s);

    printf("\nInput p: ");
    i = 0;
    while ((myChar=getchar()) != '\n')
    p[i++]=myChar;
    p[i]='\0';
    printf_s("%s\n", p);

    printf_s("return string: %s", mystrcat(s,p));   
}

char *mystrcat(char *s, char *p)
{
    int sizeOfs = 0;
    int sizeOfp = 0;
    int sizeZero = 0;

    while(*s!='\0')
    {
        sizeOfs++;
    }
    while(*p!='\0')
    {
        sizeOfp++;
    }
    for( int i=0; i<sizeOfp; i++) 
        {
            s[sizeOfs++]=p[sizeZero++];
        }
    s[sizeOfs]='\0';

    return s;
}
4

5 回答 5

3

Since this is probably a homework, here are some hints:

Inside mystrcat

  • while(*s!='\0') is an infinite loop, because s does not change inside loop's body
  • You do not need to know the size of p
  • Store s so that you could return its value
  • Move s to the end of the string using a loop
  • Copy p's characters into what's pointed to by the new s pointer until you hit '\0'
  • You are done

Outside mystrcat

  • Your mystrcat function assumes that s has enough space to store all characters of s, all characters of p, and a null terminator. Your code mallocs the space enough to hold just the null terminator. You need to change the logic to provide more space.
  • Everything that you malloc must be freed.
于 2012-11-01T15:33:51.290 回答
1

您的 malloc 只有 1 个字节,但您可能将许多字符放入 *s 和 *p。在将它们放入数组之前,您至少需要为每个字符存储空间。

于 2012-11-01T15:34:36.903 回答
0

Did you mean just this? I think there's some missing de-referencing in your example.

char *strcat (char *dest, const char *src)
{
char *dp;
char *sp = (char *)src;

if ((dest != NULL) && (src != NULL))
{
    dp = &dest[strlen(dest)];

    while (*sp != '\0')
    {
        *dp++ = *sp++;
    }
    *dp = '\0';
}
return dest;
}
于 2012-11-01T15:33:38.277 回答
0

Correct code:

char *ss=s;
while(*ss!='\0')
{
    sizeOfs++; ss++;
}
char *pp=p;
while(*pp!='\0')
{
    sizeOfp++; pp++;
}

You basicaly had an infinite loop. I would also malloc bigger size in order to not cause any memory overflows.

于 2012-11-01T15:33:51.820 回答
0

Jeniffer,我不得不承认,这看起来和我之前提到的代码非常相似

但是您缺少一些关键要素。

首先,当我做初始时malloc()

char * str1 = malloc(6); // give string 1 some memory

我这样做是为了让我要在字符串中放入足够的内容(该示例中为“hello”)。您正在尝试动态获取字符串,直到看到'\n'. 所以如果你想使用这种方法,你必须分配一些“足够大”的东西。

char *s = (char*) malloc (1);

这不能“足够大”,因为字符串必须至少有空间容纳一个字符和一个'\0',否则它不是一个字符串,只是一个字符。:)

你真的无法确定什么是“足够大”,所以如果字符串大小没有上限,那么只要抓住一些内存,如果你接近溢出它就会得到更多:

char *s = (char*) malloc (50); //That's better, room for some char's

您错过的第二点,在mystrcat()函数中,请参阅我在那里输入的行:

// We need more memory for s, it was only made to hold its own string so :
s = realloc(s, length of s + length of p + 1);  // +1 for NULL terminator

您可以阅读 realloc()但基本上它可以为您提供更多内存。当您连接字符串时,您可能需要获得更多内存。这就是计算 和 的大小的意义a所在p


第三点,我给你的伪代码是:

while(s's character is not '\0')

你在做什么是行不通的。您需要检查字符串中的字符,'\0'例如:

while(s[counter] != '\0')

并在你去的时候增加计数器。

于 2012-11-01T15:58:58.887 回答