| zguest 回复于:2001-12-14 15:58:48
|
int a[][]; a=new int[row]; delete [] a;
|
| ccb 回复于:2001-12-14 16:12:09
|
这好像是c++的,我想要的是ansi c的。
|
| mygod 回复于:2001-12-14 16:30:38
|
ansi c里面动态分配内存是可以,但是定义动态的变量好象不行,因为你想呀,ansi c里面的语法要求声明变量部分要放在函数的开头,而且这些变量是存放在堆栈里面的,堆栈里面的变量是不可能动态增大的!
|
| pierce 回复于:2001-12-14 17:46:31
|
the code below is ok!
enjoy! 
void two (char tmp[][12]) { int i; for(i = 0; i < 12; i++){ tmp[i][1] =i; } }
main(){
char* ptmp = (char*)calloc(1, 1024);
two(ptmp);
}
|
| ccb 回复于:2001-12-17 08:44:33
|
可你只是一个动态的。相当于动态一维数组
|
| pierce 回复于:2001-12-17 17:32:58
|
你再加一维不就行了.
void two (char tmp[][][1]) { tmp[n][m][0] = 9; }
|
| nico 回复于:2001-12-20 10:34:28
|
#define LIST_INIT_SIZE 100 //初始分配大小 #define LIST_INCREASEMENT 10 //分配增量 typedef struct array{ int *elem[]; //基地址 int length; //当前长度 int listsize; //当前实际被分配到的大小 }D_LIST; 使用时D_LIST a; a.elem=malloc(LIST_INIT_SIZE*sizeof(int)); 然后就能随意了。只要这么做a.elem[1][1] 这一手我没实际试过,你可以试一下,然后告诉我行不行。
|
| lchsh 回复于:2001-12-26 15:48:19
|
[这个贴子最后由lchsh在 2002/04/04 10:24am 编辑]
这样做: int ** tmp; //do something ... 可以这样试一下(2*5的数组)
tmp = (int **)malloc(len1*sizeof(int *);
for(j = 0; j < len1;j++){
tmp[j] = malloc(len2*sizeof(int));
} //len1是第一维数组的大小,len2是第二维数组的大小
//用的时候就象数组一样用了: tmp[2][3]=... //最后释放
|
| 光芒神剑 回复于:2001-12-27 21:18:30
|
是否可以考虑用连表来代替呢
|
| zzpzheng 回复于:2002-03-11 18:02:58
|
可以用链表来模拟,然后搞成一个function不就可以了嘛。 不过c中动态的东西除了链表实现起来都挺麻烦的。
|
| chejinhui 回复于:2002-03-12 09:53:53
|
标准方法:不要告诉我二维数组的两个维都不确定哦! 定义指向二维数组的指针 int (*ip)[12];//注意后一维必须确定,这样编译器才知道 ip+1时指针到底移动多少个字节。 分配时可用calloc或malloc都可以,但你必须自己计算所需字节数。
|
| zglcl008 回复于:2002-03-12 10:27:35
|
认同光芒神剑的想法。可以考虑用链表来实现。
|
| hell 回复于:2002-08-31 06:16:06
|
[这个贴子最后由Hell在 2002/08/31 06:43am 编辑]
|
| wwjxjtu 回复于:2002-10-09 19:51:15
|
int **tp,i; tp=(int *)malloc(n*sizeof(int)); for(i=0;i<n;i++) *(tp+i)=(int *)malloc(n*sizeof(int)); *(*(tp+i)+j)表示第i行,j列的值!
|
| aegis 回复于:2002-10-10 11:36:50
|
可以两个维数都不确定
对指针进行强制类型转换就可以了
|
| fanjingliang 回复于:2002-10-15 17:58:14
|
typedef int T; void Release(T **a,size_t m) { size_t row; for(row=0;row<m;row++) { if(a[row]!=NULL) { free(a[row]); } } free(a); }
T **Allocate(size_t m,size_t n) { T **a; size_t row; int success = 1; a = (T **)malloc(m*sizeof *a); if(a!=NULL) { for(row =0;row<m;row ++) { a[row]=malloc(n * sizeof *a[row]); if(a[row]==NULL) { sucess=0; } }
if(1!=sucess) { Release(a,m); a=NULL; } } return a; }
T **array; array = Allocate(row,col);
|