【问题标题】:Function to sort the names of basketball players篮球运动员姓名排序功能
【发布时间】:2015-04-17 12:25:49
【问题描述】:

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

#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. 我做错了什么。

【问题讨论】:

标签: c arrays sorting pointers malloc


【解决方案1】:

输入所有名称的循环不使用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(),这方式比可怕的永远不会被使用的gets()怪物更安全。

此外,您不会为单个名称分配任何空间,而只是为字符串指针数组分配空间。

这一行:

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

可以简化为更清晰:

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

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

【讨论】:

  • 对不起,我没听懂,你能帮我写下你提到的那一行吗?我还没有研究 strdup() 所以我认为我不能使用它,我这样写了 malloc 行,因为这就是我们的难处。
【解决方案2】:

我设法解决了! :)

这是我更新的代码:

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多