这段代码那里除了问题,c语言,题目是实现一个函数,完成两个数组元素的复制,并将新数组中的内容输出。

#include<stdio.h>
#include<string.h>
int copy(int b1[10],int b2[10])
{
strcpy(b2,b1);
return b2[10];
}
int main(void)
{
int a1[10]={"helloworld"},a2[10];
copy(a1[10],a2[10]);
printf("%s",copy(a1[10],a2[10]));
return 0;
}

你好,给你指出几个问题
1.数组a1和a2应该声明为char型数组,因为strcpy函数是应用于字符串的函数
2.char*copy(char b1[],b2[]);
3.数组a1溢出,声明为char a1[15],正确的是:
#include<stdio.h>
#include<string.h>
int copy(int b1[10],int b2[10])
{
strcpy(b2,b1);
return b2[10];
}
int main(void)
{
int a1[10]={"helloworld"},a2[10];
copy(a1[10],a2[10]);
printf("%s",copy(a1[10],a2[10]));
return 0;
}满意请采纳,谢谢追问

数组a1溢出是什么意思

追答

通俗的说就是存不下了,你的声明是:a1[10],但是“helloworld”是是一个字符(还是一个是'\0')

追问

应该怎么改呢,我的意思是整体的代码
char*copy(char b1[],b2[]);

这个的意思你能解释一下吗
另外 你看这段代码对吗?
#include
#include
int main(void)
{
char c1[10]={"helloword"},c2[10];
strcpy(c2,c1);
printf("%s\n",c2);
return 0;
}
这个我运行了就是对的

追答

char*copy(char b1[],b2[]);在这个函数中,b1和b2都是形参,大小可以不指定。。整体的代码我不是给你改出来了吗,你这个“helloworld”写的对吗,你忘写了一个字母吧???????那10个空间当然就够用了。。。

追问

对不起。。。我是新手,我还是不会,我把代码改成这样子了,还是不行,你看看那里出错了啊
#include
#include
int copy(int b1[],int b2[])
{
strcpy(b2,b1);
return b2[10];
}
int main(void)
{
int a1[10]={"hello"},a2[10];
copy(a1[10],a2[10]);
printf("%s",copy(a1[10],a2[10]));
return 0;
}

追答

哎呀,该说对不起的是我,太马虎了,粘贴错了。。。。我写的没粘给你。。。我现在把我写的给你,你参考一下:
#include
#include
char* copy(char b1[],char b2[])
{
strcpy(b2,b1);
return b2;
}
int main(void)
{
char a1[15]="helloworld",a2[10];
printf("%s\n",copy(a1,a2));
return 0;
}

追问

char* copy(char b1[],char b2[])

为什么copy前要加*

追答

因为b2的类型就是char*型的。就是说函数类型和返回值类型要匹配。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-10-21
#include<stdio.h>
#include<string.h>
int copy(int b1[],int b2[])
{
strcpy(b1,b2);
return 0;
}
int main(void)
{
char a1[11]={"helloworld"},a2[11];
copy(a2,a1);
printf("%s",a2);
return 0;
}
第2个回答  2012-10-21
int a1[10]={"helloworld"},a2[10];改为char型;

a[10]改为a[11]空间小了;
strcpy无法进行数组的复制,用for循环;
换成下面的就好了
#include<stdio.h>
#include<string.h>
int main(void)
{
int i,j;
char a1[11]={"helloworld"},a2[11];
i=strlen(a1);
for (j=0;j<i;j++)
{
a2[j]=a1[j];
}
for(j=0;j<i;j++)
{
printf("%c",a2[j]);

}
printf("\n");
return 0;
}
相似回答
大家正在搜