-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmatrix.c
32 lines (24 loc) · 858 Bytes
/
cmatrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/****************************************************************************
* Allocate a matrix of characters with dimensions n x m. Function
* returns a pointer to (0,0) element
****************************************************************************/
#include <stdlib.h>
#include "memory.h"
#include "error.h"
char **cmatrix(unsigned long n, unsigned long m) {
int i=0;
char **cm=NULL;
/* Allocate array of pointers */
if (!(cm=(char **)malloc((size_t)(n*sizeof(char *))))) {
nferrormsg("cmatrix(): Cannot allocate memory for pointer array");
return NULL;
}
/* Allocate and initialize rows */
if ((*cm=carray(n*m))==NULL) {
nferrormsg("cmatrix(): Cannot allocate memory for array of size %dx%d",n,m);
return NULL;
}
/* Set pointers to rows */
for (i=1; i<n; i++) cm[i]=cm[i-1]+m;
return cm;
}