简体   繁体   English

如何对结构数组中的字符串进行冒泡排序

[英]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 使用strcpy函数

...
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 错误。56 27 C:\\ Users ** \\ Desktop \\ Untitled1.cpp [错误]在此范围中未声明“ strcpy”

You should include <string.h> in the beginning of your source file. 您应该在源文件的开头包含<string.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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