在我的程序中,我创建了一个指向结构的指针数组(typedef为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返回的值,如果index [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
答案 0 :(得分:1)
您的代码中的主要问题是您在错误的位置;
,我确信会有疏忽。
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?。