1

I need to allocate a char** array that points to a 2 dimensions chars array.

Eventually, I want to point to a "cell" like players[playerNum][Row][Col]. This is what I wrote so far, but it fails. It is hard to understand the logic behind it, so if you can explain me whats wrong it will be great.

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

int main(int argc, char *argv[]) {
        int numOfPlayers;
        int i,j;
        char** players = (char **)malloc(sizeof(char**)*numOfPlayers); // players array
        for (i=0 ; i<numOfPlayers ; i++){
                players[i] = (char *)malloc(sizeof(char*)*10); // each player 1st D array
        }
        for (i=0 ; i<10 ; i++){
                for (j=0 ; j<10 ; j++){
                        players[i][j] = (char *)malloc(sizeof(char*)*10);      
                }
        }
        return 0;
}

1 Answer 1

4

Allocation in your code is wrong!

Do like this:

    char** players = malloc(sizeof(char*) * numOfPlayers); 
                                       ^ remove one *  
    for (i=0 ; i<numOfPlayers ; i++){
            players[i] = malloc(sizeof(char)* 10); // each player 1st D array
                                    //    ^ remove * it should be char
    }

note: sizeof(char) != sizeof(char*).
You don't need second nested for loop too. (just above code is fine) Also avoid casting return value from malloc/calloc in C

Note A mistake in your code is that numOfPlayers is not initialized (you are trying with garbage value).

Comment:

But then, i only have 2D array. i need an array that each cell of him points to a 2D array like so... you misunderstood my question

Read if you wants - a matrix of String or/ 3D char array

To allocate a matrix like: players[playerNum][Row][Col]

char ***players;
players = calloc(playerNum, sizeof(char**)); 
for(z = 0; z < playerNum; z++) { 
    players[z] = calloc(Row, sizeof(char*));
    for(r = 0; r < Row; r++) {
        players[z][r] = calloc(Col, sizeof(char));
    }
}

Edit If you wants to allocate continues memory for data, then you can use following technique. It will also be preferable because it makes less call of malloc function.

char *players_data,  // pointer to char 
     ***players; // pointer to 2D char array
players_data = calloc(playerNum * Row * Col, sizeof(char)); //1 memory for elements 
players = calloc(playerNum, sizeof(char **)); //2 memory for addresses of 2D matrices 
for(i = 0; i < playerNum; i++){
  players[i] = calloc(Row, sizeof(char*)); //3 memory for data cols
  for(r = 0; r < Row; r++){ //4  Distributed memory among rows
     players[i][r] = players_data + (i * Row * Col) + (r * Col);
  }
}

A good reference to learn: Dynamic Three Dimensional Arrays in C\C++

Sign up to request clarification or add additional context in comments.

7 Comments

thaks for your answer. Can you explain me why the * need to be removed?
@user2216190 because you need to allocate memory for that type of values you wants to store. In players you wants to store addresses of char = so just char*, In players[i] you wants to store values char
@user2216190 that is because numOfPlayers value not assigned
the numOfPlayers is initialized, and it still fails. thnaks for you help so far.
@user2216190 you didn't read answer correctly I said You don't need second nested for loop too. (just above code is fine)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.