动态分配2d数组函数 [英] dynamic allocation of 2d array function
问题描述
在主要我有一个指向另一个指针的指针分解,我打算分配一个* m array:
#include
#include< stdlib.h>
#includealloc.h
int main()
{
int ** mat,n,m;
alloc_matrix(& mat,int& n,int& m);
返回0;
在alloc.ci中有以下声明:
#ifndef ALLOC_H_INCLUDED
#define ALLOC_H_INCLUDED
#include< stdio.h>
#include< stdlib.h>
void alloc_matrix(int ***,int *,int *);
#endif
在alloc.ci中有函数:
void alloc_matrix(int *** mat,int * n,int * m)
{
printf( \\\
n =); scanf(%d,n);
printf(\\\
m =); scanf(%d,m);
* mat =(int **)calloc(* n,sizeof(int *));
int i;
for(i = 0; i <* n; i ++)
*(mat + i)=(int *)calloc(* m,sizeof(int));
}
但程序不起作用。它进入某种循环,结束。
如果我在 main
中分配它,它可以工作,但我不知道我在做什么错在 alloc
function。
这是正确的代码。你的错误是在 alloc_matrix
的定义中,你在分配循环中使用了 *(mat + i)
应该是 *(* mat + i)
as,mat是 int ***
,所以2D数组将在 * mat
中。然后你需要移动offset i
,然后解除引用1D数组的内存位置。
Main :
#include
#include< stdlib.h>
#includealloc.h
int main()
{
int ** mat,n,m;
alloc_matrix(& mat,& n,& m);
返回0;
}
alloc.h
#ifndef ALLOC_H_INCLUDED
#define ALLOC_H_INCLUDED
#include< stdio.h>
#include< stdlib.h>
void alloc_matrix(int ***,int *,int *);
#endif
alloc.c:
void alloc_matrix(int *** mat,int * n,int * m)
{
printf(\\\
n = ); scanf(%d,n);
printf(\\\
m =); scanf(%d,m);
* mat =(int **)calloc(* n,sizeof(int *));
int i;
for(i = 0; i <* n; i ++)
*(* mat + i)=(int *)calloc(* m,sizeof(int));
}
读取函数的代码:
void read_matrix(int *** mat,int n,int m)
{
int i,j; (j = 0; j< m; j ++)
对于(i = 0; i< n; i ++)
的
printf(mat [%d ] [%d] =,i,j);
scanf(%d,(*(* mat + i))+ j);
$ b $ p
$ b 问题在于它只读取第一个行,它冻结。
So i have a program in c structured in 3 files : main,alloc.h and alloc.c :
In main i have the decclaration of a pointer to another pointer to which i intend to alloc a n*m array :
#include <stdio.h>
#include <stdlib.h>
#include "alloc.h"
int main()
{
int **mat,n,m;
alloc_matrix(&mat,int &n,int &m);
return 0;
}
In alloc.c i have the following declarations :
#ifndef ALLOC_H_INCLUDED
#define ALLOC_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
void alloc_matrix(int***,int*,int*);
#endif
In alloc.c i have the function :
void alloc_matrix(int ***mat,int *n,int *m)
{
printf("\nn = "); scanf("%d", n);
printf("\nm = "); scanf("%d", m);
*mat = (int**)calloc(*n,sizeof(int*));
int i;
for(i = 0; i < *n; i++)
*(mat+i) = (int*)calloc(*m,sizeof(int));
}
But the program doesn't work.It enters some kind of loop and doesn't end.
If i allocate it in main
it would work but I have no idea what I am doing wrong in the alloc
function.
解决方案 Here is the correct code. Your error was that in the definition of alloc_matrix
, you used *(mat+i)
in the allocation loop, which should be *(*mat+i)
as, mat is a int***
so the base address for the 2D array would be in *mat
. Then you need to move by offset i
and then de-reference that memory location for the 1D array.
Main:
#include <stdio.h>
#include <stdlib.h>
#include "alloc.h"
int main()
{
int **mat,n,m;
alloc_matrix(&mat,&n,&m);
return 0;
}
alloc.h
#ifndef ALLOC_H_INCLUDED
#define ALLOC_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
void alloc_matrix(int***,int*,int*);
#endif
alloc.c :
void alloc_matrix(int ***mat,int *n,int *m)
{
printf("\nn = "); scanf("%d", n);
printf("\nm = "); scanf("%d", m);
*mat = (int**)calloc(*n,sizeof(int*));
int i;
for(i = 0; i < *n; i++)
*(*mat+i) = (int*)calloc(*m,sizeof(int));
}
The code for the read function :
void read_matrix(int ***mat,int n,int m)
{
int i,j;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
printf("mat[%d][%d] = ", i, j);
scanf("%d", (*(*mat+i))+j);
}
}
The problem with it is that it only reads the first row and the it freezes.
这篇关于动态分配2d数组函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!