1、在本地调试的时候,通常需要将二维数组当作形参传入函数中。
void findCircleNum(int** M, int MSize, int* MColSize){
/* 函数主体 */
}
int main()
{
int arr[3][3] = {{1,1,0}, {1,1,1}, {0,1,1}}; /* 定义一个二维数组 */
int *p[3]; /* 定义一个指针数组,该数组里面含有3个int类型的指针 */
int MSize = 3;
int a = 3;
int* MColSize = &a;;
p[0] = &arr[0][0]; /* 每个指针指向一个一维数组 */
p[1] = arr[1];
p[2] = arr[2];
findCircleNum(p, MSize , MColSize )); /* 调用函数 */
return 0;
}
2、函数返回值是个一维数组,需要手动申请内存
int* findCircleNum(int** M, int MSize, int* MColSize){
int *p = NULL;
p = (int *)malloc(sizeof(int) * MSize);
/* 给p指向的内存进行ch *初始化为0, 两种方式,一种安全,一种不安全 */
memset(p, 0, sizeof(p));
(void)memset_s(p, sizeof(p), 0, size); /* 其中size小于sizeof(p) */
/* 函数主体 */
}
3、函数返回值是个二维数组,需要手动申请内存
int* findCircleNum(int** M, int MSize, int* MColSize){
int **p = NULL;
int m = 2;
p = (int **)malloc(sizeof(int *) * MSize);
for (int i = 0; i < MSize; i++) {
p[i] = (int *)malloc(sizeof(int) * m);
p[i][0] = 0;
p[i][1] = 0;
}
/* 函数主体 */
}
4、使用memset函数,给数组赋值为0
void *memset(void *s, int ch, size_t n); /* 将已开辟内存空间 s 的首 n 个字节的值设为值 c */
通常用于内存初始化,也可以用来将一段内存空间设置为某个字符
5、使用memset_s函数,给数组赋值为0
/*
dest:指向要填充的对象的指针
destsz:目标数组的大小
ch:填充的字节
coun:t要填充的字节数
返回值:dest 副本,成功时为零,错误时为非零
*/
errno_t memset_s(void * dest,size_t destsz,int ch,size_t count);
与memset相同,只是在运行时检测到以下错误,并且如果dest和destsz本身有效,则在将ch存储到目标范围[dest,dest + destsz)的每个位置之后调用当前安装的约束处理函数:
dest是一个空指针
destsz或count大于SIZE_MAX
count大于destsz(会发生缓冲区溢出)
如果由dest <count <= destsz指向的字符数组的大小,行为是未定义的; 换句话说,destsz的错误值不会暴露即将发生的缓冲区溢出。