-1
typedef struct
{
char name[50];
int age;
int sex;
} Person ;
void sortAge(Person x[],int n)
{
printf("Age sort: \n");
int i,j;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (x[i].age > x[j].age)
{
int temp = x[i].age; // I change the age
x[i].age = x[j].age;
x[j].age = temp;
temp = x[i].sex; // I change the sex
x[i].sex = x[j].sex;
x[j].sex = temp;
// how I can use the same to change the names?
// tried strcpy but no work :/
}
}
}
陣列內的排序字符串使用strcpy函數如何氣泡結構
...
char temp2[50];
strcpy(temp2,x[i].name);
etc...
我得到這個錯誤..
56 27 C:\Users\**\Desktop\Untitled1.cpp [Error] 'strcpy' was not declared in this scope
使用'Person'臨時變量並使用'memcpy'複製結構,而不是結構的內容會更容易。 – AntonH
'strcpy()'的原型在''中。只是'#include '而你所說的錯誤應該會消失。因爲你正在寫頭文件,所以你可能還需要''#include ''scanf()'和'printf()';) –
pmg
'Person temp = x [i]; x [i] = x [j]; x [j] = temp;' – BLUEPIXY