【问题标题】:How do I allocate a 3d array of char pointers?如何分配 3d 字符指针数组?
【发布时间】:2019-04-13 07:07:01
【问题描述】:

我有一个 3d 字符指针数组:char ***semicols。 我希望这些价值观符合

semicol[0][0] = "ls"
semicol[0][1] = "~"
semicol[1][0] = "man"
semicol[1][1] = "grep"

等等。我有一个 char **args 数组,我在其中存储了这个,我也知道这个数组中分号的数量。我想创建具有上述结构的较小的char** ARGS,所以semicol[0] = {"ls", "~"}。 但我事先不知道每个分号参数的字符串数量,所以我不能将其设为静态char *semicols[][]。那么我如何合理地 malloc 3d 数组,还是有更好的方法来做我想做的事情?

【问题讨论】:

  • char ***semicols 不是数组,而是指向char 的指针的指针。 char **args 也一样,只是少了一个间接寻址。
  • 啊,是的,对不起,我把它们当作数组来处理,尽管这不是一个正确的术语。
  • 所以您正在尝试构建一个命令行数组以传递给execvp
  • 没错!我正在尝试实现 ;特殊标志
  • 请记住,您必须为每个间接级别分配 指针 除了最后 - 您必须为要存储的任何内容分配足够的存储空间那里。所以对于char ***semicols,你必须为命令行分配X的指针数量,Y指向每个命令中每个字符串的指针数量,然后为你分配给每个@的每个命令部分存储987654334@ 指针。 (另请注意——成为三星级程序员通常不是补充:)

标签: c arrays malloc


【解决方案1】:

您不需要 3d 字符指针数组,但需要 2d 字符指针数组。

Best way to allocate memory to a two-dimensional array in C?,您可以分配二维字符指针数组,如下所示。

char* (*semicol) [col] = malloc(sizeof(char* [row][col]));

char* (*semicol) [col] = malloc(sizeof(*semicol) * row);  //avoids some size miscomputations, especially when the destination type is later changed. //Refer chqrlie's comment.

内存分配成功后,可以semicol[i][j] = "text";

你可以通过调用free(semicol);释放分配的内存

【讨论】:

  • 更简单的尺寸计算:char* (*semicol)[col] = malloc(sizeof(*semicol) *row);
  • @chqrlie 同意。但我认为 [row][col] 更具可读性。 :)
  • 是的,在这种情况下,bith 是可读的,但替代方案更通用,它避免了一些大小错误计算,尤其是在稍后更改目标类型时。
  • 这里唯一的区别是col 为最终字符串强加了一个固定的宽度。
  • @DavidC.Rankin 不,我猜。因为它是二维指针数组。所以字符串不是这个数组的一部分。
【解决方案2】:

这是我曾经用于 3D 数组的内容。

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

int main(){
    int n = 3, m = 3;
    char ***a;

    // Malloc and store.
    a = (char***)malloc(sizeof(char**) * n);
    for(int i = 0; i <n; ++i){
        a[i] = (char**)malloc(sizeof(char*) * m);
        for(int j = 0; j < m; ++j){
            a[i][j] = "abc"; // <-- you can put your string here in place of "abc".
        }
    }

    // Print or process your array or whatever serves your purpose.
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            printf("%s\n", a[i][j]);
        }
    }

    return 0;
}

【讨论】:

  • malloc的返回不用强制转换,没必要。见:Do I cast the result of malloc?
  • 是的,很好。只是我的一个旧习惯。你可以离开演员阵容了。
  • 是的,没有问题,C++ 需要它,C 不需要。 void* 可以从 C 中的任何指针类型分配,无需强制转换。
【解决方案3】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    char ***t = malloc(sizeof(char) * 1);           // one pointer
    int i, j;

    char s[1][3][2] = {{"he", "ll", 0}};

    printf("%s\n", s[0][0]);

    for( i = 0; i < 1; ++i )
    {
        t[i] = malloc(sizeof(char) * (argc - 1));       // not including program name
        for( j = 0; j < argc - 1; ++j )
        {
            t[i][j] = calloc(strlen(argv[j + 1]) + 1, sizeof(char));        // +1 for '\0'
        }
    }

    strncpy(t[0][0], argv[1], strlen(argv[1]));
    printf("%s\n", t[0][0]);

    return 0;
}

所以我写了一些代码,测试了它,它似乎工作..我不确定这是否是你要找的

【讨论】:

  • 多个错误:malloc(sizeof(char) * 1); 分配 1 个单字节。 char s[1][3][2] = {{"he", "ll", 0}}; 不包含正确的 C 字符串。 strncpy(t[0][0], argv[1], strlen(argv[1])); 没有按照您的想法行事:您应该 1. 阅读 strncpy 的文档,2. 停止使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
相关资源
最近更新 更多