dynamic allocation from 1 instead of zero
Hi there,
I would like to allocate memory for 1d and 2d arrays in such a way that the starting index is not 0 but 1.
NR has functions to do so, but I don't want to use them because I am writing a small code that may be put in the internet and NR is not open source.
I do not necessarily need such allocation, and I could work fine with a 0-index standard C allocation, but given that I may need to couple my arrays with fortran, I'd rather work on allocation than on looping.
The standard allocation 0-indexed is clearly the following:
Code:
void iarray2d(int **mat, long nrows, long ncols, int index)
{
//nrows = number of rows
//ncols = ncolumns
//index = 1 if you want allocation in the fortran way starting from 1
//index = 0 if you want the standard C allocation way starting from 0
long i;
//Malloc pointers to rows
mat=(int **) malloc((size_t)(nrows*sizeof(int*)));
if (!mat) printf("Matrix MAT could not be allocted");
for(i=0; i<=nrows; i++)
mat[i] = malloc((size_t)(ncols*sizeof(int*)));
}
I hope to find someone willing to help. I am not a C hardcore programmer, although I use C whenever I code something.
I appreciate,
Best,
cfd