0

I have a 3d array of char pointers: char ***semicols. And I want the values to be something along the lines of

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

and so on. I have a char **args array in which I have this stored, and I also know the number of semicolons in this array. I want to create smaller char** ARGS which have the structure mentioned above, so semicol[0] = {"ls", "~"}. But I don't know the number of strings for each semicolon argument beforehand so I can't make it a static char *semicols[][]. So how do I reasonably malloc for a 3d array, or is there a better way to do what I am attempting to do?

Michael Kročka
  • 617
  • 7
  • 22
  • 1
    `char ***semicols` is not an array, but a pointer to a pointer to a pointer of `char`. Same for `char **args`, just one indirection less. – alk Apr 13 '19 at 07:11
  • Ah yes, sorry, I work with them as if they were arrays even though it is not a correct term to use. – Michael Kročka Apr 13 '19 at 07:13
  • So you are trying to build an array of command lines to pass to `execvp`? – David C. Rankin Apr 13 '19 at 07:33
  • yes exactly! I am trying to implement the usage of ; special sign – Michael Kročka Apr 13 '19 at 07:34
  • Just remember you have to allocate *pointers* for each level of indirection **except** the last -- which you must allocate sufficient storage for whatever will be stored there. So for `char ***semicols`, you must allocate `X` number of pointers for command lines, `Y` number of pointers to point to each string in each command, and then storage for each of the command parts you assign to each of the `Y` pointers. (also note -- becoming a 3-star programmer is usually not a complement `:)` – David C. Rankin Apr 13 '19 at 07:37

3 Answers3

1

You don't need 3d array of character pointers but need a 2d array of character pointers.

From Best way to allocate memory to a two-dimensional array in C?, you can allocate 2d array of character pointers as below.

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

OR

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

After successful allocation of memory, you can do semicol[i][j] = "text";

You can free the allocated memory by calling free(semicol);

MayurK
  • 1,925
  • 14
  • 27
0

Here is what I used for 3D array once.

#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;
}
SamratV
  • 224
  • 2
  • 12
-2
#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;
}

So i wrote up some code, tested it and it seems to work..i'm not sure if this is what you're looking for

simptri
  • 80
  • 9
  • Multiple bugs: `malloc(sizeof(char) * 1);` allocates 1 single byte. `char s[1][3][2] = {{"he", "ll", 0}};` does not contain proper C strings. `strncpy(t[0][0], argv[1], strlen(argv[1]));` does not do what you think: you should 1. read the documentation for `strncpy`, 2. **stop** using it. – chqrlie Apr 13 '19 at 08:19