無法使用strcpy正確地交換char數組。我使用了strncpy,memcpy,但仍然沒有得到正確的結果。使用strcpy時從'char'到'const char *'的轉換無效
這是我的程序的外觀。
class Carte
{
protected:
char Denumire[50];
char Autor[50];
char Editura[50];
int AnPublicare[50];
public:
void Citire(int n)
{
for(int i=0;i<n;i++)
{
cout<<"Denumire: "; cin>>Denumire[i]; cout<<endl;
cout<<"Autor: "; cin>>Autor[i]; cout<<endl;
cout<<"Editura: "; cin>>Editura[i]; cout<<endl;
cout<<"An Publicare: "; cin>>AnPublicare[i]; cout<<endl;
}
}
void Afisare(int n)
{
for(int i=0;i<n;i++)
{
cout<<"Denumire: "<<Denumire[i]<<endl;
cout<<"Autor: "<<Autor[i]<<endl;
cout<<"Editura: "<<Editura[i]<<endl;
cout<<"An Publicare: "<<AnPublicare[i]<<endl<<endl;
}
}
問題出在哪裏。
void Sortare(int n)
{
int ok,aux;
char *auxDen , *auxAt, *auxEd;
do{
ok=1;
for(int i=0;i<n-1;i++)
if(AnPublicare[i]>AnPublicare[i+1])
{
ok=0;
aux=AnPublicare[i];
AnPublicare[i]=AnPublicare[i+1];
AnPublicare[i+1]=aux;
strcpy(auxDen,Denumire[i]);
strcpy(Denumire[i],Denumire[i+1]);
strcpy(Denumire[i+1],auxDen);
strcpy(auxAt,Autor[i]);
strcpy(Autor[i],Autor[i+1]);
strcpy(Autor[i+1],auxAt);
strcpy(auxEd,Editura[i]);
strcpy(Editura[i],Editura[i+1]);
strcpy(Editura[i+1],auxEd);
}
}while(ok!=1);
}
};
如何使它工作?
'Denumire [i]'是'char'。你需要'&Denumire [i]'。 –
它不起作用。編譯器崩潰。 –
我懷疑編譯器崩潰了,請發佈更新後的代碼,並且過去現在得到的確切錯誤 – pm100