动态设置MAX_STRINGS

问题描述 投票:1回答:1

我如何将MAX_STRINGS分配为动态而不是静态(20)?现在,它将接受5个字符串,最大长度为200个字符,但是预期的大小应使得当用户输入3个星号(***)时,他预先输入的次数将作为MAX_STRINGS例如,如果用户之前输入2个单词,并在第三个输入提示上输入3个星号,则将2设置为MAX_STRINGS。

#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 5
#define MAX_STRING_LEN 200

void InsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN]);

int main()
{
    int index;
    char strings[MAX_STRINGS][MAX_STRING_LEN];

    /* Get input */
    printf("Enter %d strings.\n", MAX_STRINGS);
    for (index = 0; index < MAX_STRINGS; index++)
    {
        printf("Input string %d : ", index);
        scanf("%199s", strings[index]);     // limit the width so we don't go past the buffer
        strings[index][sizeof(strings[index]) - 1] = '\0';
    }

    InsertionSort(strings);

    printf("\nThe input set, in alphabetical order:\n");
    for (index = 0; index < MAX_STRINGS; index++)
    {
        printf("%s\n", strings[index]);
    }
}

void InsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN])
{
    for (int i = 1; i < MAX_STRINGS; i++)
    {
        int j = i;

        while (j > 0 && strcmp(list[j - 1], list[j]) > 0)
        {
            char tmp[MAX_STRING_LEN];
            strncpy(tmp, list[j - 1], sizeof(tmp) - 1);
            tmp[sizeof(tmp) - 1] = '\0';

            strncpy(list[j - 1], list[j], sizeof(list[j - 1]) - 1);
            list[j - 1][sizeof(list[j - 1]) - 1] = '\0';

            strncpy(list[j], tmp, sizeof(list[j]));
            list[j][sizeof(list[j]) - 1] = '\0';

            --j;
        }
    }
}
c memory-management
1个回答
0
投票

这里是经过测试的代码。请在下面找到输出:

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

#define MAX_STRINGS 100     // Number of strings should be less than 100
#define MAX_STRING_LEN 200  // Limit on length of each string

// void InsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN]);
void InsertionSort(char *list[MAX_STRINGS], int index);

int main()
{
    int index=0;
    int i;
    char *strings[MAX_STRINGS]; // Array of pointers

    /* Get Input: New code section*/

    do {
        strings[index] = malloc(MAX_STRING_LEN * sizeof(char));
        printf("Input string %d : ", index);
        scanf("%199s", strings[index]);
        strings[index][sizeof(strings[index]) - 1] = '\0';
    } while(strcmp(strings[index++], "***")!=0);

    InsertionSort(strings, index-2);  // change. -2 because the last valid 
                      // index will be excluding *** and extra post-increment

    printf("\nThe input set, in alphabetical order:\n");
    for (i = 0; i < index-1; i++)   // change. -1 to exclude ***
    {
        printf("%s\n", strings[i]);
    }
}

void InsertionSort(char *list[MAX_STRINGS], int index)   // change
{
    for (int i = 0; i <= index; i++)
    {
        int j = index;  // change

        while (j > i && strcmp(list[j - 1], list[j]) > 0)   //change
        {
            char tmp[MAX_STRING_LEN];
            strncpy(tmp, list[j - 1], sizeof(tmp) - 1);
            tmp[sizeof(tmp) - 1] = '\0';

            strncpy(list[j - 1], list[j], sizeof(list[j - 1]) - 1);
            list[j - 1][sizeof(list[j - 1]) - 1] = '\0';

            strncpy(list[j], tmp, sizeof(list[j]));
            list[j][sizeof(list[j]) - 1] = '\0';

            --j;
        }
    }
}

很少测试:

$ ./a.out 
Input string 0 : cd
Input string 1 : ab
Input string 2 : ***

The input set, in alphabetical order:
ab
cd



$ ./a.out 
Input string 0 : gh
Input string 1 : ef
Input string 2 : cd
Input string 3 : ab
Input string 4 : ***

The input set, in alphabetical order:
ab
cd
ef
gh

MAX_STRINGS现在是100。这意味着最多可以有100个字符串。因此,***最多只能位于第99个索引位置。 malloc只是为了避免不必要的内存浪费。该代码很容易理解。请通过。如果您需要更多说明,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.