求修改 复制字符串的问题

C语言 码拜 8年前 (2016-04-18) 717次浏览
#include<stdio.h>
char copying(char *p)
{
char *p1;
while(*p!=”\0″)
{
*p1=*p;
p1++;
p++;
}
*p1=”\0″;
return *p1;
}
main()
{
char *p,s[80];
printf(“please input astring:\n”);
scanf(“%s”,s);
*p=copying(s);
printf(“%s”,*p);
}
解决方案

10

#include<stdio.h>
#include “string.h”
void copying(char *p)
{
char p1[80];
int i=0;
int clen=strlen(p);//获得长度
while(i<clen)
{
p1[i]=*p;
p++;
i++;
}
for(int j=0;j<clen;j++)
{
printf(“%c”,p1[j]);
}
printf(“\n”);
}
main()
{
char *p,s[80];
printf(“please input astring:\n”);
gets(s);
copying(s);
}
帮你修改了一下。

10

#include<stdio.h>
char copying(char *p)
{
char *p1;
while(*p!=”\0″)
{
*p1=*p;//这是一个错误的做法。p1声明后,没有分配内存块,直接指向*p的值.p1会把值存放在哪里,这是一个未知的.
p1++;
p++;
}
*p1=”\0″;//这里j是什么意思?
return *p1;//返回”\0″?
}
main()
{
char *p,s[80];
printf(“please input astring:\n”);
scanf(“%s”,s);//这里不能用scanf,题主可以调试一下,假如用scanf获取字符串。然后把s做为实参给copying函数,会发现只得到两个字符。strlen(p)也得不到一个正常的长度。
*p=copying(s);   //copying函数里最后return *p1  是返回”\0″,题主你知道吗?就算copying函数里前面的步骤不出错,这里也是错误的。
printf(“%s”,*p);
}

10

不看其他的,首先main函数里char *p,*p=…就错了

20

1.返回的应该是char *
2.应该在copying函数里malloc一块空间用来存放拷贝的字符串
3.用一个指针记录p1最初始的位置,原因是你之后有++操作,p1指向会后移

#include<stdio.h>
#include<stdlib.h>
char *copying(char *p)
{
	char *p1 = (char *)malloc(80);
	char *res = p1;
	while(*p!="\0")
	{
		*p1=*p;
		p1++;
		p++;
	}
	*p1="\0";
	return res;
}
void main()
{
	char *p,s[80];
	printf("please input astring:\n");
	scanf("%s",s);
	p=copying(s);
	printf("%s\n",p);
}

20

第一,字符串是char*,字符是char
第二,你主函数中的的p都没有申请内存,你不能直接使用*p
所以:

#include<stdio.h>
char* copying(char *p)
{
	char *p1;
	while(*p!="\0")
	{
		*p1=*p;
		p1++;
		p++;
	}
	*p1="\0";
	return p1;
}
main()
{
	char *p,s[80];
	printf("please input astring:\n");
	scanf("%s",s);
	p=copying(s);
	printf("%s",*p);
}

10

引用:

四楼正解   可是本人之前很少用到*res   啥时候要用这个

只是保存一下p1最初始的地址,否则的话你之后++了,return的时候返回的是最后一个\0的地址

5

引用:

楼上的不对啊 有错

你copying函数里也要分配内存,上面代码没帮你改

5

  1   #include<stdio.h>                                                                                                                                                                                      
    1     
    2 #define    MAX_LEN    128
    3     
    4 char *copying(char *p)
    5 {   
>>  6     char *p1 = malloc(sizeof(char) * MAX_LEN);
    7     if (NULL == p1) {    //判断申请结果
>>  8     |   fprintf(stderr, "malloc Error: %s\n", strerror(errno));
    9     |   return NULL;
   10     }
   11     char *ptmp = p1;
   12     while(*p!="\0") {
   13     |   *ptmp++ = *p++;
   14     }
   15     *ptmp = "\0";    //字符串结束标志,添加上
   16     
   17     return p1;
   18 }   
   19 int main(void)
   20 {   
   21     char *p = NULL, s[80];
   22     printf("please input astring:\n");
   23     scanf("%s", s);
   24     p = copying(s);
   25     printf("%s\n", p);
>> 26     free(p);    //用完释放
   27     
   28     return 0;
   29 }

5

#include<stdio.h>
#include<stdlib.h>
char * copying(char *p)
{
	char *p1,*p2;//申请一个p2用来记录p1的起始位置,原因是后面p1的志向发生了变化,返回时要返回p2
	p2=p1=(char *)calloc(1,100);
	while(*p!="\0")
	{
		*p1=*p;
		p1++;
		p++;
	}
	*p1="\0";
	return p2;
}
void main()
{
	char *p=NULL,s[80];
	printf("please input astring:\n");
	scanf("%s",s);
	p=copying(s);
	printf("%s\n",p);	//输出的是p而不是*p
}

5

引用:
#include<stdio.h>
#include<stdlib.h>
char * copying(char *p)
{
	char *p1,*p2;//申请一个p2用来记录p1的起始位置,原因是后面p1的志向发生了变化,返回时要返回p2
	p2=p1=(char *)calloc(1,100);
	while(*p!="\0")
	{
		*p1=*p;
		p1++;
		p++;
	}
	*p1="\0";
	return p2;
}
void main()
{
	char *p=NULL,s[80];
	printf("please input astring:\n");
	scanf("%s",s);
	p=copying(s);
	printf("%s\n",p);	//输出的是p而不是*p
}

内存用完之后可以free掉,切记free的是内存而不是指针


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求修改 复制字符串的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)