Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 指针数组的动态分配_C_Arrays_Pointers_Dynamic Allocation - Fatal编程技术网

C 指针数组的动态分配

C 指针数组的动态分配,c,arrays,pointers,dynamic-allocation,C,Arrays,Pointers,Dynamic Allocation,我对C语言的代码有问题。 我需要输入用enter按钮分隔的姓名列表。 当用户输入单词QUIT时,输入停止。 程序需要按字母顺序打印姓名列表,所有字母均为小写 名称的数量以及每个名称的长度未知,需要动态分配。此外,如果名称在输入中出现多次,则在输出中只应出现一次 下面是代码应如何运行的示例: Please enter a list of names: john chris ben chris david QUIT 共有4个名称: ben chris david john 我考

我对C语言的代码有问题。 我需要输入用enter按钮分隔的姓名列表。 当用户输入单词QUIT时,输入停止。 程序需要按字母顺序打印姓名列表,所有字母均为小写

名称的数量以及每个名称的长度未知,需要动态分配。此外,如果名称在输入中出现多次,则在输出中只应出现一次

下面是代码应如何运行的示例:

Please enter a list of names:

john

chris

ben

chris

david

QUIT
共有4个名称:

ben

chris

david

john
我考虑过使用一个动态分配的指针数组,其中每个指针都包含每个名称,并且是动态分配的。问题是我不知道如何编写它而不出现运行时错误

注意:在这一点上,我不允许使用我还没有学会的东西,比如结构和递归,并且只能使用stdio.h、stdlib.h和string.h库

提前谢谢

下面是代码,它不完整,但我在这一点上得到了一个运行时错误:

char **nameList;
int i = 0, j = 0, size = 0, check = 0;
printf("Please enter list of names:\n");
//allocate one cell of memory to the list
nameList = (char**)malloc(sizeof(char));
if (nameList == NULL)
{
    printf("Cannot allocate Memory");
    return 0;
}
//Add the first letter to the first string in the array
nameList[i][j] = getchar();
size += sizeof(char);
while (check != 1)
{
    //check if current entered letter is not an enter
    while (nameList[i][j] != '\n')
    {
        //allocated another char sized memory to the string
        nameList = (char**)realloc(nameList, (size + sizeof(char)));
        if (nameList == NULL)
        {
            printf("Cannot allocate Memory");
            return 0;
        }
        j++;
        //adding another char to the current string
        nameList[i][j] = getchar();
        size += sizeof(char);
    }
    j = 0;
    if (nameList[i][j] == 'Q')
    {
        if (nameList[i][j + 1] == 'U')
            if (nameList[i][j + 2] == 'I')
                if (nameList[i][j + 3] == 'T')
                    check++;
    }
    i++;
}
nameList=char**reallocknamelist,size+sizeofchar

嗯。。。你为什么用“+”呢?您也在寻找错误类型值的大小


这将分配一个指针数组。但是这里的指针指向什么呢?

请通读并阅读。您提到一个运行时错误,是什么代码导致了这个错误?我们可以帮助您解决您试图解决的特定问题,但堆栈溢出不是一种代码编写服务。好的,您应该能够通过动态分配指针和字符串数组来完成。因此,您应该显示当前代码和错误。我将否决任何剥夺该学生学习机会的答案。@Evi-如果您显示您尝试过的代码,即代码,您希望该代码执行的操作,您将有更多机会获得帮助,以及您得到的任何结果,特别是当它们与您期望的结果不同时。因此,您可以使用string.h库函数。在string.h中,以下两个函数对您非常有用:strcmp和strcpy。我使用“+”是因为我想在每次输入字母时向字符串添加另一个char大小的内存,只要字符串不是“QUIT”。主要的问题是,我不知道这样做是否正确,以及realloc是否应该用于数组列表和每个字符串。每个指针都应该指向我输入的每个字符串,当输入为enter时,每个字符串都是分开的。字符串确实需要单独分配。数组中的每个项都是指向字符串的指针。因此,名称列表[0]是指向字符串的指针,需要分配该字符串。namelist[1]是指向namelist[2]到namelist[i]的下一个字符串的指针,依此类推。对于初学者来说,跟踪指针到指针是相当困难的,所以我有点担心接下来的建议可能会让你困惑。但你还是会发现一个问题。nameList是指向char而不是char的char*指针列表,您需要使用sizeofchar*