向后显示动态数组的内容

时间:2013-10-16 20:48:44

标签: c dynamic-arrays

因此该程序应该执行以下操作:
要求用户输入字符串(字符串的最大长度为250),一旦用户输入任何内容(因此基本上点击'enter'),程序将停止接收输入并继续显示用户向后输入的每个字符串。 /> 这是我的代码 - 一切正常,但我无法进入底部的while循环。

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

int main()
{
    char userInput[250];
    char *newSpace;
    char *array;
    int stringLength;
    int inputElems = 0;
    //Reset startingPtr back to where 'array' begins
    char *startingPtr = array - inputElems;
    // Sets endPtr to the last input element in the array
    char *endPtr = startingPtr + inputElems;

    do
    {
        puts("Please enter a string!");
        // Gets input from user
        fgets(userInput, 250, stdin);
        // Finds out the number of characters the user entered
        // + 1 to make room for the null character
        stringLength = strlen(userInput) + 1;
        // Allocate memory with the needed space
        newSpace = malloc(stringLength);
        // If malloc didn't allocate memory, display error
        if (NULL == newSpace)
        {
            puts("Error!");
        }

        // Copy user input into array
        strcpy(newSpace, userInput);
        // Save input into array
        array = newSpace;

        // For testing purposes only, this line will be deleted once I can access the array
        //puts("Here's what you wrote");
        //printf("%s", array);

        // Increase array
        array++;
        // Increase number of times user has input something
        inputElems++;

    } while(stringLength != 2);

    //puts("Testing outside");

    while (endPtr > startingPtr)
    {
        //puts("Testing inside");
        --endPtr;
        printf("%s", *endPtr);
    }

    free(newSpace);
}

2 个答案:

答案 0 :(得分:1)

您的while循环体永远不会执行,因为您永远不会在整个程序的生命周期内修改endPtrstartingPtr,它们都指向同一位置。因此,endPtr > startingPtr永远不会成真。

此外,array++无法按预期方式工作:array指向用户所写内容的第一个字符,而array++只是将其向前移动到下一个字符。我想你想要一个char *的数组。因此,将char *array;更改为char *array[250];(假设用户最多可输入250个句子)。

当然,由于无法增加数组,因为数组不是可修改的l值,所以还必须保​​留最后写入索引的计数。但我发现你已经有了inputElems,这应该足够了。这是修改后的代码:

int main()
{
    char userInput[250];
    char *newSpace;
    char *array[250];
    int stringLength;
    int i;
    int inputElems = 0;

    do
    {
        puts("Please enter a string!");
        // Gets input from user
        fgets(userInput, 250, stdin);
        // Finds out the number of characters the user entered
        // + 1 to make room for the null character
        stringLength = strlen(userInput) + 1;
        // Allocate memory with the needed space
        newSpace = malloc(stringLength);
        // If malloc didn't allocate memory, display error
        if (NULL == newSpace)
        {
            puts("Error!");
        }

        // Copy user input into array
        strcpy(newSpace, userInput);
        // Save input into array
        array[inputElems] = newSpace;

        // For testing purposes only, this line will be deleted once I can access the array
        //puts("Here's what you wrote");
        //printf("%s", array);

        // Increase number of times user has input something
        inputElems++;

    } while(stringLength != 2);

    //puts("Testing outside");

    for (i = inputElems-1; i >= 0; i++)
    {
        //puts("Testing inside");
        printf("%s", array[i]);
        free(array[i]);         
    }
    return 0;
}

请注意代码末尾的更新循环。它从头到尾遍历数组。请注意,我更改了您调用free()的位置。由于我们在循环中分配内存,对于array中的每个位置,我们还必须free()每个位置,因此这也必须在循环内进行。

答案 1 :(得分:0)

确定。你需要一个指针数组,如:char * arr [10];

你可以在这里存储从arr [0]到arr [9]的10个字符串。 (好吧不是直接10个字符串,而是指向它们的指针)

arr [0] =(char *)malloc(sizeof(char * length_of_string));

strncpy(arr [0],your_string,length_of_string);

只需将arr [9]打印到arr [0]。

MFG