指针的打印有关问题
指针的打印问题
代码如下 : 打印出错,求大神指点!
#include <stdio.h>
#include <string.h>
char *app(char *string1, char *string2)
{
char s1[20] = {0}, s2[20] = {0};
strcpy(s1,string1);
strcpy(s2,string2);
return strcat(s1,s2);
}
void main(void)
{
char *p1 = "abcd";
char *p2 = "cde";
char *p;
p = app(p1,p2);
printf(p);
printf("\n");
}
ukbug
11 years, 5 months ago
Answers
出现乱码是引文,返回的是局部指针的吧!函数返回的是s1的地址,局部指针!
修改了下,看看
#include <stdio.h>
#include <string.h>
char *app(char *string1, char *string2)
{
char s2[20] = {0};
// strcpy(s1,string1);
strcpy(s2,string2);
return strcat(string1,s2);
}
int main(void)
{
char p1[20] = "abcd";
char *p2 = "cde";
char *p;
p = app(p1,p2);
printf("%s\n", p);
return 0;
}
wslf123
answered 11 years, 5 months ago