有人可以帮我处理这段代码。我需要将这两个指针附加在一起,但它对我不起作用。该代码不会将指针添加在一起。我认为 *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;
}