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

    }

使用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

錯誤。56 27 C:\\ Users ** \\ Desktop \\ Untitled1.cpp [錯誤]在此范圍中未聲明“ strcpy”

您應該在源文件的開頭包含<string.h>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM