如何在结构数组中冒充排序字符串

时间:2014-03-29 18:03:15

标签: c sorting struct char strcpy

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

1 个答案:

答案 0 :(得分:2)

  

错误.. 56 27 C:\ Users ** \ Desktop \ Untitled1.cpp [错误]'strcpy'未在此范围内声明

您应该在源文件的开头加入<string.h>