简体   繁体   English

如何传递指针以在堆上创建二维数组?

[英]How can I pass a pointer to create a 2-D array on heap?

I learnt to create heap allocation of 2-D char array and initialize it. 我学习了如何创建二维char数组的堆分配并对其进行初始化。

Method 1: 方法1:

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

int main()
{
    char ** arr;
    arr = (char **) malloc(2 * sizeof(char *));
    arr[0] = (char *) malloc(256 * sizeof(char));
    arr[1] = (char *) malloc(256 * sizeof(char));

    sprintf(arr[0], "%s", "This is string 1");
    sprintf(arr[1], "%s", "This is string 2");

    int i;
    for(i = 0; i < 2; i++)
    {
        printf("%s\n", arr[i]);
    }

    return 0;
}

But I'm trying to learn is to pass the pointer to a function to create a 2-D array, but in vain. 但是我想学习的是将指针传递给函数以创建二维数组,但是徒劳。

Method 2: 方法2:

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

void test(char *** ptr);

int main()
{
    char ** arr;
    test(&arr);

    sprintf(arr[0], "%s", "This is string 1");
    sprintf(arr[1], "%s", "This is string 2");

    int i;
    for(i = 0; i < 2; i++)
    {
    printf("%s\n", arr[i]);
    }

    return 0;
}

void test(char *** ptr)
{
    **ptr = (char **) calloc(2, sizeof(char *));
    *ptr[0] = (char *) malloc(256 * sizeof(char));
    *ptr[1] = (char *) malloc(256 * sizeof(char));
}

Something wrong with the way I'm doing it in Method 2 . 我在方法2中执行此操作的方式有问题。 Please help me understand the way of doing heap allocation of 2-D array by passing pointers. 请帮助我通过传递指针来理解二维数组堆分配的方式。 Thanks. 谢谢。

On the first allocation you're not using the proper level of indirection. 在第一次分配时,您没有使用适当的间接级别。 You want *ptr , not **ptr . 您想要*ptr ,而不是**ptr

For the second and third allocations, operator precedence is getting you. 对于第二个和第三个分配,运算符优先级可以帮助您。 The array index operator [] has higher precedence than the dereference operator * so you need parenthesis to first dereference, then index the array: 数组索引运算符[]优先级比取消引用运算符*优先级高,因此您首先需要加括号,然后再对数组进行索引:

void test(char *** ptr)
{
    *ptr = calloc(2, sizeof(char *));
    (*ptr)[0] = malloc(256 * sizeof(char));
    (*ptr)[1] = malloc(256 * sizeof(char));
}

Rather than using a triple pointer (which as you found out can be confusing), return the allocated value instead and assign that to your variable: 而不是使用三重指针(您会发现这会造成混淆),而是返回分配的值并将其分配给变量:

char **test()
{
    char **ptr = calloc(2, sizeof(char *));
    ptr[0] = malloc(256 * sizeof(char));
    ptr[1] = malloc(256 * sizeof(char));
    return ptr;
}

...

arr = test();

Note that this is much cleaner. 请注意,这更加清洁。

Also, don't cast the return value of malloc / calloc / realloc . 另外, 不要calloc malloc / calloc / realloc的返回值

Due to operator precedence the expression *ptr[0] is parsed as *(ptr[0]) which is not really correct. 由于运算符的优先级 ,表达式*ptr[0]被解析为*(ptr[0]) ,这实际上是不正确的。

You need to use explicit parentheses to overcome it as in (*ptr)[0] . 您需要使用显式括号来克服它,如(*ptr)[0]


As an unrelated side-note, being called a three star programmer is usually not a compliment. 无关紧要的是,被称为三星级程序员通常不是一种夸奖。

A note: 一张纸条:

*ptr[0] == *(ptr[0]) == **ptr

therefore 因此

*ptr[1] == *(ptr[1]) == *(*(ptr+1)) == unknown memory

Fixed code: 固定代码:

void test(char *** ptr)
{
    *ptr      = (char** ) calloc(2, sizeof(char *));
    (*ptr)[0] = (char*  ) malloc(256 * sizeof(char));
    (*ptr)[1] = (char*  ) malloc(256 * sizeof(char));
}

Explanation: Explanation Image 说明: 说明图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM