Allocate memory to char *** in C

前端 未结 3 1068
悲&欢浪女
悲&欢浪女 2020-12-07 03:32

So, I\'m having trouble in allocating memory for a char *** type variable. My objective is to create a matrix of strings and the code I currently have for memor

相关标签:
3条回答
  • 2020-12-07 03:45

    As you have said, you were able to successfully create an array of strings using your below code:

    char **list;
    list = calloc(n, sizeof(char *));
    for (j = 0; j < n; j++){
    list[j] = calloc(MAX_STR, sizeof(char));
    }
    

    Now, you need an array of array of strings, so it should be:

    char ***matrix;
    
    matrix = calloc(n, sizeof(char**)); //This creates space for storing the address of 'n' array of strings 
    for(z = 0; z < n; z++) { //This loop creates the 'n' array of strings.
        matrix[z] = calloc(n, sizeof(char*));
        for(i = 0; i < n; i++) {
            matrix[z][i] = calloc(MAX_STR, sizeof(char));
        }
    }
    

    So, basically, in the second code, you are just creating space for storing 'n' lists. Hope this makes it clear.

    0 讨论(0)
  • 2020-12-07 03:52

    Assuming you want to allocate storage for n arrays, each with n strings, each up to MAX_STR long, there are a couple of mistakes in the code

    matrix = calloc(n*MAX_STR, sizeof(char**));
    

    should be

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

    and

    for(i = 0; i < MAX_STR; i++) {
    

    should be

    for(i = 0; i < n; i++) {
    

    In a little more detail,

    matrix = calloc(n*MAX_STR, sizeof(char**));
    for(z = 0; z < n; z++) {
    

    seems wrong. You allocate n*MAX_STR elements but only use n of them

    matrix[z] = calloc(n, sizeof(char*));
    for(i = 0; i < MAX_STR; i++) {
    

    is also questionable and is wrong for n<MAX_STR. (You allocate n elements then write to MAX_STR of them.)

    Finally, depending on whether you consider MAX_STR to include space for a null terminator, you may need to change

    matrix[z][i] = calloc(MAX_STR, sizeof(char));
    

    to

    matrix[z][i] = calloc(MAX_STR+1, 1);
    
    0 讨论(0)
  • 2020-12-07 03:57

    Matrix of String or/ 3D char array:

    Suppose you need N matrices, each matrix can store R strings of length MAX_STR-1 then you should allocated memory your loop as follows, like:

    char ***matrix;
    
    matrix = calloc(N, sizeof(char**)); 
    for(z = 0; z < N; z++) { 
        matrix[z] = calloc(R, sizeof(char*));
        for(i = 0; i < R; i++) {
            matrix[z][i] = calloc(MAX_STR, sizeof(char));
        }
    }
    

    Its will create matrix like:

    matrix
    +-------------------+------------------+-----------------------+
    | 0                 | 1                | 2                     |
    +-------------------+------------------+-----------------------+        
     |                           |                        |
     ▼                           ▼                        ▼
    +--+    +----------+       +--+    +----------+     +--+    +----------+
    |0 +---►| MAX_STR  |       |0 +---►| MAX_STR  |     |0 +---►| MAX_STR  |
    +--+    +----------+       +--+    +----------+     +--+    +----------+
    |1 +---►| MAX_STR  |       |1 +---►| MAX_STR  |     |1 +---►| MAX_STR  |
    +--+    +----------+       +--+    +----------+     +--+    +----------+
    |2 +---►| MAX_STR  |       |2 +---►| MAX_STR  |*    |2 +---►| MAX_STR  |
    +--+    +----------+       +--+    +----------+     +--+    +----------+
    |3 +---►| MAX_STR  |       |3 +---►| MAX_STR  |     |3 +---►| MAX_STR  |
    +--+    +----------+       +--+    +----------+     +--+    +----------+
    |4 +---►| MAX_STR  |       |4 +---►| MAX_STR  |     |4 +---►| MAX_STR  |
    +--+    +----------+       +--+    +----------+     +--+    +----------+
    |5 +---►| MAX_STR  |       |5 +---►| MAX_STR  |     |5 +---►| MAX_STR  |
    +--+    +----------+       +--+    +----------+     +--+    +----------+
    |6 +---►| MAX_STR  |       |6 +---►| MAX_STR  |     |6 +---►| MAX_STR  |
    +--+    +----------+       +--+    +----------+     +--+    +----------+
    |7 +---►| MAX_STR  |       |7 +---►| MAX_STR  |     |7 +---►| MAX_STR  |
    +--+    +----------+       +--+    +----------+     +--+    +----------+
     ^        ^ 
     |        |
     |        matrix[z][i]
     matrix[z]
    

    Here N = 3, and
    R = 8

    Its char 3D array of size matrix[N][R][MAX_STR]

    Suppose, if someone wants to printf string I marked * in diagram, that is third string in second array, then he/she need to index like

    printf("%s",matrix[1][2]);
    

    Although answer is accepted I am updating my answer so onw can find it helpful in future

    0 讨论(0)
提交回复
热议问题