c - 使用 strcmp 使用指向结构的指针数组对数据进行排序

标签 c arrays sorting struct

在我的程序中,我创建了一个指向结构体的指针数组(类型定义为 Person),并按邮政编码升序对信息进行排序。

这是应该对我的数据进行排序的函数:

void sortdata(Person * arr[], int noElements) {
    /* temporary pointer to Person data type to aid with swapping */
    Person * tempptr = (Person * ) malloc(sizeof(Person));

    int i, j, compare;

    for (i = 0; i <= (noElements - 1); i++); {
        for (j = (i + 1); j <= noElements; j++) {
            compare = strcmp(arr[i] - > zip, arr[j] - > zip);
            if (compare > 0) {
                printf("attempted sort %d times.\n", j);
                /* stores value in index i for array inside of temporary pointer  */
                strcpy(tempptr - > name, arr[i] - > name);
                strcpy(tempptr - > address, arr[i]);
                strcpy(tempptr - > citystate, arr[i] - > address);
                strcpy(tempptr - > zip, arr[i] - > zip);

                /* swaps values */
                strcpy(arr[i] - > name, arr[j] - > name);
                strcpy(arr[i] - > address, arr[j] - > address);
                strcpy(arr[i] - > citystate, arr[j] - > citystate);
                strcpy(arr[i] - > zip, arr[j] - > zip);

                strcpy(arr[j] - > name, tempptr - > name);
                strcpy(arr[j] - > address, tempptr - > address);
                strcpy(arr[j] - > citystate, tempptr - > citystate);
                strcpy(arr[j] - > zip, tempptr - > zip);
            }

        }
    }
}

我添加了 printf 语句来告诉我程序实际进入循环的次数。我设置的变量等于 strcmp 返回的值,如果索引 [i] 处的邮政编码大于数组索引 [j] 处的邮政编码,则该变量为正。如果是这种情况,我将输入排序并交换两个索引中的值,但我的程序永远不会进入循环。我在这里做错了什么?

结构体定义:

typedef struct person{
char name[50];
char address[50];
char citystate[30];
char zip[10];
}Person;

分配内存的函数:

int takedata(Person *arr[])
{
/* counter variable */
int i = 0;
char teststring[25];

while ((gets(teststring)) != NULL && i < 50)
{
    /* dynamically allocates memory for each index of the array */
    arr[i] = (Person *)malloc(sizeof(Person));

    /* takes in data from user/ input file */

    strcpy(arr[i]->name, teststring);
    gets(arr[i]->address);
    gets(arr[i]->citystate);
    gets(arr[i]->zip);

    i++;    
}



    printf("Processed %d sets of data.\n\n", i);

    return (i-1);
}

示例数据:

A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
B1, B2
19831 Henshaw St
Culver City, CA
94023
C1, C2
5142 Dumont Pl
Azusa, CA
91112
D1, D2
20636 De Forest St
Woodland Hills, CA
91364
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
E1, E2
4851 Poe Ave
Woodland Hills, CA
91364
F1, F2
20225 Lorenzana Dr
Los Angeles, CA
91111
G1, G2
20253 Lorenzana Dr
Los Angeles, CA
90005
H1, H2
5241 Del Moreno Dr
Los Angeles, CA
91110
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
J1, J2
5135 Quakertown Ave
Thousand Oaks, CA
91362
K1, K2
720 Eucalyptus Ave 105
Inglewood, CA
89030
L1, L2
5021 Dumont Pl
Woodland Hills, CA
91364
M1, M2
4819 Quedo Pl
Westlake Village, CA
91362
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
N1, N2
20044 Wells Dr
Beverly Hills, CA
90210
O1, O2
7659 Mckinley Ave
Los Angeles, CA
90001

最佳答案

代码中的主要问题是您的 ; 位于错误的位置,我确信是由于疏忽所致。

for (i = 0; i <= (noElements - 1); i++); {
                                       ^^ Remove the ;

它结束第一个 for 循环。这打乱了你的逻辑。

关于@DavidHoelzer 的评论:

您可以替换这些行:

strcpy(tempptr - > name, arr[i] - > name);
strcpy(tempptr - > address, arr[i]);
strcpy(tempptr - > citystate, arr[i] - > address);
strcpy(tempptr - > zip, arr[i] - > zip);

/* swaps values */
strcpy(arr[i] - > name, arr[j] - > name);
strcpy(arr[i] - > address, arr[j] - > address);
strcpy(arr[i] - > citystate, arr[j] - > citystate);
strcpy(arr[i] - > zip, arr[j] - > zip);

strcpy(arr[j] - > name, tempptr - > name);
strcpy(arr[j] - > address, tempptr - > address);
strcpy(arr[j] - > citystate, tempptr - > citystate);
strcpy(arr[j] - > zip, tempptr - > zip);

tempptr = arr[i];
arr[i] = arr[j];
arr[j] = tempptr;

其中 tempPtr 简单声明为:

Person * tempptr = NULL;

注意

强烈建议不要使用gets。 阅读 Why is the gets function so dangerous that it should not be used? .

关于c - 使用 strcmp 使用指向结构的指针数组对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30315897/

相关文章:

c - 执行C程序时如何传递文件名

c - 在内核模块中访问串口

ios - Swift:如何对数组中结构的子值使用 UISearchResultsUpdating?

javascript - <thead> 元素中的数据表排序和过滤

sql-server - 如何按字母顺序对字符串进行排序

scala - 按有序索引对列表进行排序

c - 学习 GNU C 2.4 cowsdf?

c - 在 C 中返回一个 typedef

c++ - 函数声明错误 - 返回一个指针数组

java - 打印数组和 toString 时的输出问题