简体   繁体   中英

How to bubble sort string within an array of structures

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 :/
            }
        }

    }

Using strcpy function

...
char temp2[50];
strcpy(temp2,x[i].name);
etc...

I get this error..

56  27  C:\Users\**\Desktop\Untitled1.cpp   [Error] 'strcpy' was not declared in this scope

error.. 56 27 C:\\Users**\\Desktop\\Untitled1.cpp [Error] 'strcpy' was not declared in this scope

You should include <string.h> in the beginning of your source file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM