Hi everyone,
What is the difference between a pointer to a function that returns a pointer, to a function that takes a pointer as argument?
1) Passing a pointer to a function and returning it after operations
Code:
void iarray2d(int **mat, size_t nrows, size_t 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= malloc((nrows+index)*sizeof(int*));
if (!mat){
printf("Matrix MAT could not be allocted");
exit(1);
}
mat[0] = NULL;
for(i=index; i<nrows+index; i++){
mat[i] = malloc((ncols+index)*sizeof(int*));
if (!mat[i]){
printf("Matrix MAT could not be allocted\n The exec. will Exit now\n");
exit(1);
}
}
return;
}
2) returning a pointer from a pointer to function
Code:
int **iarray2d(size_t nrows, size_t ncols, int index)
{
long i;
int **mat;
//Malloc pointers to rows
mat= malloc((nrows+index+1)*sizeof(int*));
if (!mat){
printf("Matrix MAT could not be allocted");
exit(1);
}
mat[0] = NULL;
for(i=index; i<nrows+index; i++){
mat[i] = malloc((ncols+index+1)*sizeof(int*));
if (!mat[i]){
printf("Matrix MAT could not be allocted\n The exec. will Exit now\n");
exit(1);
}
}
return mat;
}
Thank you in advance
All the best
cfd