C 编纂词典

C 编纂词典,c,string,pointers,visual-studio-2012,multidimensional-array,C,String,Pointers,Visual Studio 2012,Multidimensional Array,我有一个拼贴作业来构建一个动态字典,这个作业必须与指针和动态分配一起使用(我知道我在这里说的是一些琐碎的东西)。 无论我是如何尝试使用代码的,这就是我所想到的。 我肯定我在这里有错误,我的问题实际上是如何正确实现内存分配,然后如何在我分配的内存中存储单词/定义 printf("Please enter the amount of words in the dictionary.\n"); scanf("%d",&numOfWords); getchar(); wordsArr = (c

我有一个拼贴作业来构建一个动态字典,这个作业必须与指针和动态分配一起使用(我知道我在这里说的是一些琐碎的东西)。 无论我是如何尝试使用代码的,这就是我所想到的。 我肯定我在这里有错误,我的问题实际上是如何正确实现内存分配,然后如何在我分配的内存中存储单词/定义

 printf("Please enter the amount of words in the dictionary.\n");
scanf("%d",&numOfWords);
getchar();
wordsArr = (char***)malloc(3 * sizeof(char**));
for (int i = 0; i < numOfWords; i++)
{
    printf("Please enter the word and how many defenitions it has (1 or 2).\n");
    scanf("%s %d",&wBuffer[i],&defs);
    getchar();
    wordsArr[i]=(char**)malloc(strlen(wBuffer)*sizeof(char*)+1);
    strcpy(*wordsArr[i],wBuffer);
    for (int j = 0; j < defs; j++)
    {
        printf("Enter the %d is:\n",i);
        gets(dBuffer);
        wordsArr[i][j]=(char*)malloc(strlen(dBuffer)*sizeof(char)+1);
        strcpy(wordsArr[i][j],dBuffer);
    }
}
//Dont mind this just for the print test    
for (int i = 0; i < numOfWords; i++)
{
    for (int j = 0; j < defs; j++)
    {
        printf("%s",wordsArr[i][j]);
    }
}
printf(“请输入字典中的字数。\n”);
scanf(“%d”&numOfWords);
getchar();
wordsArr=(char***)malloc(3*sizeof(char**));
for(int i=0;i
}

也许可以试试这个(手边没有C编译器)

char***数组;
数组=malloc(numOfWords*sizeof(char**));
对于(i=0;i

这将使numOfWords指针的内存分配到一个字符数组数组,其中有3个字符数组,第一个是字典单词,另外两个是定义。

为什么不使用哈希表…或数组列表…或某些数据结构老师限制我们不使用任何相关内容,只使用函数、指针、字符串、数组和动态分配。有趣的东西。你知道数据结构都可以建立在数组、poitner、字符串和动态分配的基础上,但这是我内心邪恶的学生,我想把它交给老师。如果我可以使用数据结构,我会得到这个“项目”,但因为我不能使用它们,我被卡住了,上面的代码是我目前唯一能想到的。。。基本上我需要的是X个单词,每个单词都有1或2个防御(arr[][])。差不多吧。我知道上面的代码是一个很大的WTF,因为它不工作。2d数组,第一个维度是单词编号,第二个维度是定义编号。你所拥有的大部分似乎是正确的,但不是很有活力。完成后,请确保释放malloced内存
char ***array;
array = malloc(numOfWords * sizeof(char**));
for (i = 0; i < rows; i++)
   array[i] = malloc(3 * sizeof(char*)); // assumign 2 is your max definitions