-1

My goal is to have a an array of arrays so I can get the size from another variable and append as many objects as I need in a for loop later.

Sorta like this:

char *test[3][1][3] = {
    {"FOO", "BAR"},
    {"BIZ", "NIZ"},
    {"BIZ", "NIZ", "NAZ"}
};

    
printf("\nTESTNG: \n");
printf("TEST: %s\n", test[0][0][0]);
printf("TEST: %s\n", test[0][0][1]);
// this is the only value that is dynamic, the rest are key value pairs that are being inserted
// like FOO : BAR
// this function returns an integer
int array_size = someotherfunction();

char** people = (char**) malloc(array_size);
for(i = 0; i < array_size; i++){
        people[i] = (char*)malloc(2);
}
people[0][0][0] = "FOO";
printf("Person: %s", people[0][0][0]);
user3505901
  • 408
  • 1
  • 6
  • 19
  • What does `someotherfunction` do? What does it return? Remember that [`malloc`](https://en.cppreference.com/w/c/memory/malloc) allocates an amount of ***bytes***, not an amount of "elements". To allocate space for two `char *`, you must use `2 * sizeof(char *)` to get the size in bytes. – Some programmer dude Jul 05 '20 at 03:45
  • Furthermore, `people[0][0][0] = "FOO"` doesn't make sense, doesn't the compiler shout warnings at you for that? Not to mention that the pointer `people[0][0}` doesn't point anywhere. You have a "2D" array of pointers, not a "3D" array. – Some programmer dude Jul 05 '20 at 03:47
  • And remember that `malloc` only allocates memory, it doesn't initialize it in any way. – Some programmer dude Jul 05 '20 at 03:47
  • Lastly, in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jul 05 '20 at 03:48
  • `someotherfunction` just returns an integer, but it's different every time depending on circumstances. I know that part is fine – user3505901 Jul 05 '20 at 03:52
  • What is the semantic value of what `somefunction` returns? Does it return the number of *elements*, or the number of *bytes*? It *seems* like it returns the number of elements, which is the wrong value to pass to `malloc`. – Some programmer dude Jul 05 '20 at 03:55
  • 2
    If I had a dollar for every time the "part I know is fine" was the problem... – Retired Ninja Jul 05 '20 at 04:00
  • oh, yes...it returns just an integer value of elements the idea was to create an array of size x that could hold smaller arrays of things like `{"FOO", "BAR"}` – user3505901 Jul 05 '20 at 04:02
  • In C, there is no need to cast the return of `malloc`, it is unnecessary. See: [Do I cast the result of malloc?](http://stackoverflow.com/q/605845/995714) – David C. Rankin Jul 05 '20 at 06:03
  • 1
    I sorta think you should compile with `-Wall` or similar... – Antti Haapala -- Слава Україні Jul 05 '20 at 06:34

1 Answers1

0

Right now you have 4D char array. The following code is sufficient for your data.There is no point of extra dimension if size of that dimension is one.

    char *test[3][3] = {
        {"FOO", "BAR"},
        {"BIZ", "NIZ"},
        {"BIZ", "NIZ", "NAZ"}
    };
printf("TEST: %s\n", test[0][0]);
printf("TEST: %s\n", test[0][1]);

Edited:

Following is for char* test[NumElement]. Similarly you can implement for 3d array.

char **test = malloc(sizeof(char*) * NumElement);
for (i = 0; i < NumElement; i++)
{
    test[i] = malloc((STR_Length+1)* sizeof(char));  //for "FOO" STR_Length is 3, +1 for null char
}

Edited2: exact code

int N = 3, M = 3, STRLEN=3;
char *** test = (char *** )malloc(N * sizeof(char ** )) ;  //equal to char * test[N][M]
for(int i = 0 ; i < N ; i++ ) //Allocate memroy for each row
{ 
    test[i] = (char ** ) malloc(M * sizeof(char * )) ;
    for ( int j = 0 ; j < M ; j++ )
    { 
        test[i][j] = (char *) malloc ((STRLEN+1) * sizeof(char));
    }
 }
 test[0][0] = "FOO";
 test[0][1] = "BAR" ;
 printf("%s\n",test[0][0]);
 printf("%s\n",test[0][1]);
Neeraj Bansal
  • 380
  • 7
  • 23