复制字符串函数,复制的字符串不在函数本身中打印
copy string function, copied string not printing in function itself
我正在编写一个使用指针动态复制字符串的程序。该函数是从 main()
调用的,复制的字符串显示在 main()
中,但是当我尝试在函数本身中显示复制的字符串时,只有光标闪烁。
这是我的代码:
#include<stdio.h>
#include<stdlib.h>
void copystr(char *p,char *s);
int main ()
{
char str[50],*ptr,*p;
printf("enter the string\n");
gets(str);
ptr=malloc(50*sizeof(char));
copystr(ptr,str);
getch();
return 0;
}
void copystr(char *p,char *s)
{
s=malloc(50*sizeof(char));
while(*s!='[=10=]')
{ *p=*s;
s++;
p++;
}
*p='[=10=]';
printf("copy string is %s", p); // if i display this printf in main 'p' prints fine, but here no output
free(p);
}
可能是什么原因?
问题
- 永远不要使用
gets()
。它受到缓冲区溢出问题的严重影响。请改用 fgets()
。
sizeof(char)
保证是C
中的1
。乘以 sizeof(char)
实际上是多余的。
- 首先,您接收
s
作为 copystr()
中的传入参数之一,然后您立即执行 s=malloc(50*sizeof(char));
。在这里,您丢失了传入指针。所有进一步的操作都是没有意义的。
- 如果失败,
malloc()
returns NULL。在这种情况下进一步使用返回的指针将导致 undefined behaviour.
malloc()
returns 未初始化的内存,如果成功的话。 malloc()
后直接写while(*s!='[=22=]')
也就是读取未初始化内存的内容。调用 undefined behaviour.
- 在
copystr()
中,您正在递增接收到的指针 p
,并在复制后添加空终止符。所以 p
的当前值指向空终止符。因此,在 printf()
中使用 p
根本不会给你任何输出。
解决方案
- 在
malloc()
之后对 ptr
添加 NULL 检查以检查是否成功。
- 删除
copystr()
内 s
上的 malloc()
。
- 使用另一个临时指针来保存
p
的传入值。使用 p
获取存储的值后,使用临时指针打印复制的字符串。
您不需要函数中的 malloc - 它会删除您传递给 str 中函数的数据 - 尝试重新运行您的代码,去掉函数中的那一行
删除行
s=malloc(50*sizeof(char));
此外,您可能还想在 main 中放置一些东西,以便在调用复制例程后打印出复制的函数,这样您就知道它有效了。 (还要注意在其他答案中指出的所有 p++ 操作之后使用指针 p 打印字符串的问题)
工作代码在下面进行了这些更改 - 请注意最好不要使用 gets...scanf 完成工作...
#include<stdio.h>
#include<stdlib.h>
void copystr(char *p,char *s);
int main ()
{
char str[50],*ptr,*p;
printf("enter the string\n");
// gets(str);
scanf("%s",str);
ptr=malloc(50*sizeof(char));
copystr(ptr,str);
printf("copy string is %s\n", ptr); // if i display this printf in main 'p' prints fine, but here no output
free(ptr);
// getch();
return 0;
}
void copystr(char *p,char *s)
{
while(*s!='[=11=]')
{ *p=*s;
s++;
p++;
}
*p='[=11=]';
}
让它在函数中工作......见
#include<stdio.h>
#include<stdlib.h>
void copystr(char *p,char *s);
int main ()
{
char str[50],*ptr,*p;
printf("enter the string\n");
// gets(str);
scanf("%s",str);
ptr=malloc(50*sizeof(char));
copystr(ptr,str);
free(ptr);
// getch();
return 0;
}
void copystr(char *p,char *s)
{
int i=0;
while(*s!='[=12=]')
{ *(p+i)=*s;
s++;
i++;
}
*(p+i)='[=12=]';
printf("copy string is %s\n", p); // if i display this printf in main 'p' prints fine, but here no output
}
printf("copy string is %s", p); // if i display this printf in main 'p' prints fine, but here no output
因为 p
没有指向该函数中字符串的开头(即使假设您用 s=malloc(50*sizeof(char))
解决了这个问题)。
此外,如果您:
free(p);
就像你现在拥有它一样,你也不能在 main 中使用原来的 ptr
。
我正在编写一个使用指针动态复制字符串的程序。该函数是从 main()
调用的,复制的字符串显示在 main()
中,但是当我尝试在函数本身中显示复制的字符串时,只有光标闪烁。
这是我的代码:
#include<stdio.h>
#include<stdlib.h>
void copystr(char *p,char *s);
int main ()
{
char str[50],*ptr,*p;
printf("enter the string\n");
gets(str);
ptr=malloc(50*sizeof(char));
copystr(ptr,str);
getch();
return 0;
}
void copystr(char *p,char *s)
{
s=malloc(50*sizeof(char));
while(*s!='[=10=]')
{ *p=*s;
s++;
p++;
}
*p='[=10=]';
printf("copy string is %s", p); // if i display this printf in main 'p' prints fine, but here no output
free(p);
}
可能是什么原因?
问题
- 永远不要使用
gets()
。它受到缓冲区溢出问题的严重影响。请改用fgets()
。 sizeof(char)
保证是C
中的1
。乘以sizeof(char)
实际上是多余的。- 首先,您接收
s
作为copystr()
中的传入参数之一,然后您立即执行s=malloc(50*sizeof(char));
。在这里,您丢失了传入指针。所有进一步的操作都是没有意义的。 - 如果失败,
malloc()
returns NULL。在这种情况下进一步使用返回的指针将导致 undefined behaviour. malloc()
returns 未初始化的内存,如果成功的话。malloc()
后直接写while(*s!='[=22=]')
也就是读取未初始化内存的内容。调用 undefined behaviour.- 在
copystr()
中,您正在递增接收到的指针p
,并在复制后添加空终止符。所以p
的当前值指向空终止符。因此,在printf()
中使用p
根本不会给你任何输出。
解决方案
- 在
malloc()
之后对ptr
添加 NULL 检查以检查是否成功。 - 删除
copystr()
内s
上的malloc()
。 - 使用另一个临时指针来保存
p
的传入值。使用p
获取存储的值后,使用临时指针打印复制的字符串。
您不需要函数中的 malloc - 它会删除您传递给 str 中函数的数据 - 尝试重新运行您的代码,去掉函数中的那一行
删除行
s=malloc(50*sizeof(char));
此外,您可能还想在 main 中放置一些东西,以便在调用复制例程后打印出复制的函数,这样您就知道它有效了。 (还要注意在其他答案中指出的所有 p++ 操作之后使用指针 p 打印字符串的问题)
工作代码在下面进行了这些更改 - 请注意最好不要使用 gets...scanf 完成工作...
#include<stdio.h>
#include<stdlib.h>
void copystr(char *p,char *s);
int main ()
{
char str[50],*ptr,*p;
printf("enter the string\n");
// gets(str);
scanf("%s",str);
ptr=malloc(50*sizeof(char));
copystr(ptr,str);
printf("copy string is %s\n", ptr); // if i display this printf in main 'p' prints fine, but here no output
free(ptr);
// getch();
return 0;
}
void copystr(char *p,char *s)
{
while(*s!='[=11=]')
{ *p=*s;
s++;
p++;
}
*p='[=11=]';
}
让它在函数中工作......见
#include<stdio.h>
#include<stdlib.h>
void copystr(char *p,char *s);
int main ()
{
char str[50],*ptr,*p;
printf("enter the string\n");
// gets(str);
scanf("%s",str);
ptr=malloc(50*sizeof(char));
copystr(ptr,str);
free(ptr);
// getch();
return 0;
}
void copystr(char *p,char *s)
{
int i=0;
while(*s!='[=12=]')
{ *(p+i)=*s;
s++;
i++;
}
*(p+i)='[=12=]';
printf("copy string is %s\n", p); // if i display this printf in main 'p' prints fine, but here no output
}
printf("copy string is %s", p); // if i display this printf in main 'p' prints fine, but here no output
因为 p
没有指向该函数中字符串的开头(即使假设您用 s=malloc(50*sizeof(char))
解决了这个问题)。
此外,如果您:
free(p);
就像你现在拥有它一样,你也不能在 main 中使用原来的 ptr
。