指向动态指针数组的指针指向指向字符串的动态指针数组

pointer to dynamic array of pointers to dynamic array of pointer that points to strings

我对第一个动态指针数组中的值进行初始化有问题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char*** GetIndexes()
{
int n = 0;
char ***index;
printf("please insert the number of words you want to add to dictionary\n");
scanf("%d", &n);
index = (char***)calloc(n, sizeof(char));
if (index == NULL)
{
    printf("allocation Failed");
    return;
}
return index;
}
char** GetDefinitions()
{
int n = 0;
char **definition;
printf("please insert the number of defintions you want to add to the word\n");
scanf("%d", &n);
definition = (char**)calloc(n+1, sizeof(char));
if (definition == NULL)
{
    printf("allocation failed");
    return;
}
return definition;
}
int main()
{
char *** dptr = GetIndexes();
if (dptr == NULL)
{
    printf("memory Allocation failed");
}
int indexcount = sizeof(dptr) / sizeof(char),i;
for (i = 0; i < indexcount; i++)
{
    printf("word number %d\n", i + 1);
    *dptr[i] = GetDefinitions();
}
printf("%p",dptr);

}

我尝试了 运行 VS2013 中的调试器,在我输入了我希望它崩溃的定义数后,出现了这条消息:

ConsoleApplication1.exe 中 0x01103FB0 处的未处理异常:0xC0000005:访问冲突写入位置 0x00000000。

我错过了一些东西的分配,但我不太明白我错过了什么,

提前致谢

正如评论中已经提到的那样,这是一个非常糟糕的主意,在这里只使用双指针就很好。但如果您想使用指针分配内存,则应完成以下修复

index = calloc(n, sizeof(char));

应该是

index = calloc(n, sizeof(char **));

definition = calloc(n+1, sizeof(char));

应该是

definition = calloc(n+1, sizeof(char *));

你的程序很烂

  1. 你分配 n char ***s 但只请求 space n chars 并且也为 char **,为了防止这种错误,你可以这样使用 sizeof 运算符

    char ***index;
    index = calloc(n, sizeof(*index));
    

    char **definition;
    definition = calloc(n, sizeof(*definition));
    

    如您所见,转换 calloc 会使它变得更难,而且没有必要。

  2. 你有一个 return 声明,它 return 与 GetIndexes() 以及 GetDefinitions.[=35= 中的任何内容都不一样]

    如果您想在调用函数中处理失败,他们应该 return NULL

    return NULL;
    
  3. 您错误地使用了 sizeof 运算符来确定分配给

    char *** 指针的数量
    int indexcount = sizeof(dptr) / sizeof(char)
    

    这将是 48,具体取决于体系结构,即指针的大小总是除以 1 sizeof(char) == 1

    您无法计算该值,您只需跟踪它即可。尺码

  4. 您两次取消引用三重指针并尝试为其分配一个双指针

    *dptr[i] = GetDefinitions();
    

    这里的运算符优先级也是一个问题,但无论如何,这是错误的,可能是你的意思

    dptr[i] = GetDefinitions();
    
  5. 这不会使您的程序崩溃,但在退出程序之前 free 所有 malloced 指针肯定很重要。

这里有一个建议可以让您的代码正常工作,请忽略它的用途,因为不清楚您要做什么

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

char ***GetIndexes(unsigned int *count)
{
    char ***index;

    printf("please insert the number of words you want to add to dictionary > ");
    scanf("%u", count);

    index = calloc(*count, sizeof(*index));
    if (index == NULL)
    {
        printf("allocation Failed");
        return NULL;
    }

    return index;
}

char **GetDefinitions(unsigned int *count)
{
    char **definition;

    printf("please insert the number of defintions you want to add to the word > ");
    scanf("%u", count);

    definition = calloc(*count + 1, sizeof(*definition));
    if (definition == NULL)
    {
        printf("allocation failed");
        return NULL;
    }
    return definition;
}

int main()
{
    unsigned int indexCount, i;
    char      ***dptr = GetIndexes(&indexCount);

    if (dptr == NULL)
    {
        printf("memory Allocation failed");
    }

    for (i = 0; i < indexCount; i++)
    {
        unsigned int definitionsCount;

        printf("Word number %u\n", i + 1);
        dptr[i] = GetDefinitions(&definitionsCount);
        if (dptr[i] != NULL)
        {
            /* use dptr[i] here or maybe somewhere else, but when you finish */
            free(dptr[i]);
        }
    }
    printf("%p", dptr);
    /* now if you are done using dptr */
    free(dptr);

    return 0;
}