作者discipile (孤獨的風)
看板C_and_CPP
標題[問題] strcpy的練習程式
時間Sun Nov 1 23:04:11 2009
遇到的問題: (題意請描述清楚)
目前我在練習strcpy的使用,但是compile就不過了,錯誤在下面會一一列出
希望得到的正確結果:
將str2複製到str1
程式跑出來的錯誤結果:
D:\Dev-Cpp\mywork\strcpy.cpp In function `int main()':
9 D:\Dev-Cpp\mywork\strcpy.cpp invalid conversion from `char' to `char*'
9 D:\Dev-Cpp\mywork\strcpy.cpp initializing argument 1 of `char
strcpy(char*, char*)'
9 D:\Dev-Cpp\mywork\strcpy.cpp invalid conversion from `char' to `char*'
9 D:\Dev-Cpp\mywork\strcpy.cpp initializing argument 2 of `char
strcpy(char*, char*)'
9 D:\Dev-Cpp\mywork\strcpy.cpp At global scope:
18 D:\Dev-Cpp\mywork\strcpy.cpp ISO C++ forbids declaration of `strcpy' with
no type
D:\Dev-Cpp\mywork\strcpy.cpp In function `int strcpy(char*, char*)':
18 D:\Dev-Cpp\mywork\strcpy.cpp new declaration `int strcpy(char*, char*)'
3 D:\Dev-Cpp\mywork\strcpy.cpp ambiguates old declaration `char strcpy(char*,
char*)'
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
Dev-C++
有問題的code: (請善用置底文標色功能)
#include<stdio.h>
#include<stdlib.h>
char strcpy(char str1[],char str2[]);
int main ()
{
int i;
char str1[5]={1,2,3,4,5};
char str2[5]={0,0,0,0,0};
strcpy(str1[5],str2[5]);
for(i=0;i<=5;i++)
printf("%d",str1[i]);
for(i=0;i<=5;i++)
printf("%d",str2[i]);
system("pause");
}
strcpy(char str1[],char str2[])
{
int i=0;
for(i=0;str2[i]!='\0';i++)
str1[i]=str2[i];
}
補充說明:
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.66.168.12
推 ledia:strcpy 本身有一點小問題, 不過最大的問題在於 11/01 23:06
→ ledia:strcpy 是給字串 (型態是 char *), 但你給的是字元, 而且還 11/01 23:07
→ ledia:超過陣列的界限..., 可能要弄清楚陣列的概念 11/01 23:07
→ discipile:有超過?不是5個複製到另外5個去? 11/01 23:13
→ discipile:對喔!是字串,感謝^^ 11/01 23:14
→ netsphere:可能還要弄清楚指標的概念 11/01 23:14
→ discipile:我感到這些觀念薄弱了,近期將書翻一翻 11/01 23:22