彭春霞:C里面动态定义数组?

来源:百度文库 编辑:中科新闻网 时间:2024/04/30 07:20:53
我现在定义的是A[5][5]
后来要加几个元素A[6][6]

#include <stdio.h>
#include <stdlib.h>
#define SIZE 128
#define N_SIZE 256
void main(){
int *ptr;
ptr=(int *)malloc(100*sizeof(int));//1
ptr=(int *)realloc(ptr,200*sizeof(int));//2
free(ptr);
}
注;1)malloc (calloc)确保空间 SIZE
2)realloc 在原空间基础上增加N_SIZE
一般在前面会有一些对SIZE的判断,来决定是否增加.
啦队这里就不写啦

动态数组?ArrayList??
如果是指ArrayList的话.

System.Collections.ArrayList NewArr = new System.Collections.ArrayList() ; //定义一个新的数组
object o = new System.Data.DataSet() ; //数组中可以予object以下所有类;
NewArr.Add(o) ; //向其中添加.
object oLoad = (object)NewArr[0] ; //读出时还原成原类型.
NewArr.Remove(o) ; //移除它.

这个问题问过多次了。。。
int *array=(int*)malloc(sizeof(int)*size)
或int *array=(int*)calloc(size,sizeof(int))

只能是用链表来实现。

int abc[];