动态指向数组动态2维数组? [英] Dynamic pointing array to a dynamic 2 dimensions array?
问题描述
我需要分配一个char **数组,指向2维数组的字符。
最后,我想指出一个细胞就像玩家[playerNum] [行] [COL]
。
这是我写了这么远,但它失败。
这是很难理解它背后的逻辑,所以如果你能解释我什么是错,将是巨大的。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主(INT ARGC,CHAR *的argv []){
INT numOfPlayers;
INT I,J;
焦炭**玩家=(字符**)的malloc(sizeof的(字符**)* numOfPlayers); //播放器阵列
对于(i = 0; I< numOfPlayers;我++){
玩家[i] =(字符*)malloc的(sizeof的(字符*)* 10); //每个玩家第一个二维数组
}
对于(I = 0; I&小于10;我++){
为(J = 0; J&小于10; J ++){
玩家[I] [J] =(字符*)malloc的(sizeof的(字符*)* 10);
}
}
返回0;
}
分配是错误的!
这样做:
的char **玩家=的malloc(sizeof的(字符*)* numOfPlayers);
^删除一个*
对于(i = 0; I< numOfPlayers;我++){
玩家[i] =的malloc(sizeof的(字符)* 10); //每个玩家第一个二维数组
// ^ *删除它应该是char
}
请注意:的sizeof(字符)= sizeof的(字符*)
结果
你不需要第二个嵌套循环了。 (略高于code是罚款)
也避免在C 距离的malloc /释放calloc铸造返回值>
注意在code的一个错误是, numOfPlayers
未初始化(你正在试图用垃圾值)。
注释:
不过呢,我只有二维数组。我需要一个数组,他的每一个细胞指向一个二维数组,像这样......你误解了我的问题。
块引用>阅读,如果你想 - 一个<一个href=\"http://stackoverflow.com/questions/16522035/allocate-memory-to-char-in-c/16522223#16522223\">matrix字符串或/ 3D字符数组
要分配矩阵,如:
玩家[playerNum] [行] [COL]
字符***的玩家;
玩家=释放calloc(playerNum,sizeof的(字符**));
对于(Z = 0; z,其中,playerNum; Z ++){
玩家[Z] =释放calloc(行,sizeof的(字符*));
为(R = 0;为r行; R ++){
玩家[Z] [R] =释放calloc(西,sizeof的(炭));
}
}修改如果您想要分配内存持续数据,那么你可以使用下面的技术。这也将是preferable因为它使得malloc函数的调用更少。
的char * players_data,//字符指针
***的玩家; //指向二维字符数组
players_data =释放calloc(playerNum *行*上校,的sizeof(字符)); // 1内存元素
玩家=释放calloc(playerNum,sizeof的(字符**)); // 2内存二维矩阵地址
对于(i = 0; I&LT; playerNum;我++){
玩家[i] =释放calloc(行,sizeof的(字符*)); // 3内存用于数据COLS
为(R = 0;为r行; R ++){// 4分布式行之间存储器
玩家[I] [R] = players_data +(我*行*列)+(R * COL);
}
}一个很好的参考学习:用C \\ C 动态三维数组++
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; }
解决方案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 CNote 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++
这篇关于动态指向数组动态2维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!