C++ “创造自己的”;charcopy";使用指针的函数

C++ “创造自己的”;charcopy";使用指针的函数,c++,pointers,C++,Pointers,我正在尝试创建自己的char copy(比如strcopy)函数需要自定义,我不能使用std库(Strcopy)中的一个。I无法将我的字符数组更改为字符串。我不能使用内置函数来复制字符。我必须使用指针来操作数组。 我正试图弄清楚真正的strcopy是什么样子的,但我似乎无法让我的工作正常进行。我认为我想创建一个指向strcpy(char destination,char source)的指针,然后我通过source操作destination,但没有正确复制,我只是打印垃圾。请帮助我接受所有建议

我正在尝试创建自己的char copy(比如strcopy)函数需要自定义,我不能使用std库(Strcopy)中的一个。I无法将我的字符数组更改为字符串。我不能使用内置函数来复制字符。我必须使用指针来操作数组。

我正试图弄清楚真正的strcopy是什么样子的,但我似乎无法让我的工作正常进行。我认为我想创建一个指向strcpy(char destination,char source)的指针,然后我通过source操作destination,但没有正确复制,我只是打印垃圾。请帮助我接受所有建议

void StrCpy(char *destination, char *source);

int main()
{
card card1[10], card2[10];
card1[0].cvalue = 1000;
card2[0].cvalue = 90000;
card *card1p = &card1[0];
card *card2p = &card2[0];
//set up card file to be read in
ifstream fin;
string finName;

//get file name from user
cout << "Enter file name...(cardFile.txt)" << endl;;
getline(cin,finName);


//open the file
fin.open(finName.c_str());


//check if cardFile.txt opens correctly
if(!fin.good())
{
    cout << "Error with card file" << endl;
    return 0;
}
else
{
    card *deckPointer = NULL;

    //prime fin
    //fin >> deck[i].suit;
    //fin >> deck[i].rank;
    //fin >> deck[i].cvalue;

    while(fin.good())
    {
    for(card1p = &card1[0]; card1p < &card1[10];card1p++)
        {
            fin >> (*card1p).suit;
            fin >> (*card1p).rank;
            fin >> (*card1p).cvalue;
        }   
    }    
}            
StrCpy((*card2p).suit, (*card1p).suit);
cout << (*card2).suit << endl;

}

void StrCpy(char *destination,char *source)
             {
             char *p = destination;
             *p = *source;


             }
void StrCpy(字符*目的地,字符*来源);
int main()
{
卡片1[10],卡片2[10];
card1[0]。cvalue=1000;
card2[0]。cvalue=90000;
卡片*card1p=&card1[0];
卡片*card2p=&card2[0];
//设置要读入的卡文件
流鳍;
字符串finName;
//从用户获取文件名
cout-deck[i].秩;
//fin>>甲板[i]。C值;
while(fin.good())
{
对于(card1p=&card1[0];card1p<&card1[10];card1p++)
{
fin>>(*card1p).套装;
fin>>(*card1p).等级;
fin>>(*card1p).c值;
}   
}    
}            
StrCpy((*card2p).套装,(*card1p).套装);
库特
这是不对的。基本上你在做
目标[0]=源[0]
,它不是实际副本


你应该仔细阅读指针。和格式。而
std::string

通常函数的定义方式如下

char * StrCpy( char *destination, const char *source )
{
    char *p = destination;

    while ( *p++ = *source++ );

    return destination;
}
另一个认识

char * StrCpy( char *destination, const char *source )
{
    char *p = destination;

    do
    {
        *p++ = *source;   
    } while ( *source++ );

    return destination;
}
还有一个

char * StrCpy( char *destination, const char *source )
{
    for ( char *p = destination; *p = *source; ++p, ++source );

    return destination;
}


不幸的是,你有这些限制,因为这不是你在现代C++中的做法。还要注意的是,人们通常会使用
card1p->suit
代替
(*card1p).suit
->
是访问指向类的指针的成员和方法的正常语法。但是,例如,此副本甚至不复制第一个“H”。我不能用弦乐手。你能帮我吗?这就是它的作用。小贴士:你需要使用一个循环。我说过要退出,但我不能使用这个。你能帮忙吗me@mattmowris我不明白你为什么不能使用它。@Vladfrommoskow不为人们做家庭作业。@mattmowris编程有一件事:一般来说,做“简单”操作的方法不多,比如复制字符串。这里,Vlad的代码基本相同,只是循环结构略有不同。如果你做得对,它将有点像图书馆的功能。@mattmowris“它必须不同于内置的功能”-你应该立即到你就读的任何学校的管理办公室,并获得退款。
char * StrCpy( char *destination, const char *source )
{
    for ( char *p = destination; *p = *source; ++p, ++source );

    return destination;
}
char * StrCpy( char *destination, const char *source )
{
    char *p = destination;

    while ( *p = *source ) ++p, ++source;

    return destination;
}