用于对篮球运动员姓名进行排序的功能

时间:2015-04-17 12:25:50

标签: c arrays sorting pointers malloc

我需要编写一个程序来接收(来自用户)篮球队中的球员数量,然后我需要以动态的方式创建阵列。程序将按字母顺序对数组进行排序,然后将其打印出来。 我写了这段代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define LENGTH 20

int main(void)
{
    int numplayers, i, j;
    char persname[LENGTH], tname[LENGTH], temp[LENGTH];

    printf("Please insert the amount of basketball players in the group\n");
    scanf("%d", &numplayers);

    char **players = (char **) malloc(numplayers * sizeof(char *));

    printf("Please insert the names of your %d basketball players\n", numplayers);
    for (i = 0; i < numplayers; i++) {
        gets(persname);
        strcpy(persname[i], tname[i]);
    }

    for (i = 0; i < numplayers-1; i++) {
        for (j = i+1; j < numplayers; j++) {
            if (strcmp(persname[i], persname[j])) {
                strcpy(temp[i], persname[i]);
                strcpy(persname[i], persname[j]);
                strcpy(persname[j], temp);
            }
        }
    }

    for (i = 0; i < numplayers; i++) {
        printf("%s\t\t%s\n", tname[i], persname[i]);
    }

    return 0;
}

但是当我运行代码时,我在输入团队中的玩家数量后立即收到错误Unhandled exception at 0x507340E3 (msvcr120d.dll) in Question4.exe: 0xC0000005: Access violation reading location 0xFFFFFFCC. 我做错了什么。

2 个答案:

答案 0 :(得分:2)

输入所有名称的循环不会使用players。相反,它错误地使用pernametname。这一行:

strcpy(persname[i], tname[i]);

不应该编译,你以一种没有任何意义的方式混合类型。您应该输入一行,然后将新内存动态分配到players[i]并将输入复制到那里。如果你有strdup(),那就是它的优点。

基本上输入循环应该是这样的:

for (i = 0; i < numplayers; i++)
{
    char line[1024];
    if(fgets(line, sizeof line, stdin) != NULL)
    {
       const size_t len = strlen(line);
       players[i] = malloc(len + 1);
       if(players[i] == NULL)
       {
         fprintf(stderr, "**Out of memory!\n");
         exit(1);
       }
       memcpy(players[i], line, len + 1);
    }
    else
      fprintf(stderr, "**I/O error!\n");
}

这使用fgets() way 比可怕的永远不会被使用的gets()怪物更安全。

此外,您还没有为单个名称分配任何空间,仅用于字符串指针数组。

这一行:

char** players = (char**)malloc(numplayers*sizeof(char*));

可以简化为更清晰:

char** players = malloc(numplayers * sizeof *players);

无需重复类型名称和no need to cast the return value of malloc()

答案 1 :(得分:0)

我设法解决了! :)

这是我更新的代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define LENGTH 20
#define LENGTH2 20
int main(void)
{
    int numplayers, i, j;
    char persname[LENGTH][LENGTH2], temp[LENGTH];
    printf("Please insert the amount of basketball players in the group\n");
    scanf("%d", &numplayers);
    char** players = (char**)malloc(numplayers*sizeof(char));
    printf("Please insert the names of your %d basketball players\n", numplayers);
    for (i = 0; i < numplayers+1; i++)
    {
        gets(persname[i]);
    }
    for (i = 1; i < numplayers+1; i++)
    {
        for (j = 1; j < numplayers+1; j++)
        {
            if (strcmp(persname[j - 1], persname[j]) > 0)
            {
                strcpy(temp, persname[j - 1]);
                strcpy(persname[j - 1], persname[j]);
                strcpy(persname[j], temp);
            }
        }
    }
    printf("\nBasketball players names in order are : ");
    for (i = 0; i < numplayers+1; i++)
    {
        puts(persname[i]);
    }
    getch();
    free(players);
    system("PAUSE");
    return 0;
}
相关问题