Hi there, I am writing a code composed of a main function calling another function (called CSR in the wrapped code) whose arguments are pointers to 1D arrays.
The pointers are to be modified in length inside the function by means of realloc(), and returned to the main program with the new size and new elements.
The problem is that, although the function fills the arrays correctly ( I verify this by printing the array inside the function), such arrays are not passed back to the main correctly, in fact if I print the array from inside main(), the elements are not correct. It seems, from the output, that the memory allocated for the arrays is freed before the arrays are returned to main.

Array Jc and Ac should look like (I only put the first 10 elements of the total 57):
Jc = 1, 2, 4, 1, 2, 3, 4, 5, 2, 3, 5, 6, ...
Ac = 101, 102, 103, 104, 105, 106, ...

but when printed from inside main(), I get the following instead:
Jc = 134524928, 2, 4, 57, 1, 3, 9, 13, ...
Ac = 101, 0, 0, 0, 0, 0, 0, ....

I wrap my code here, I hope someone can help
Thanks
S.

Main():
Code:
#include<stdio.h>
#include<stdlib.h>

#include"nrutil.h"
#include"CSR.h"

main()
{
	int i,j,k, nz=0;

	double A[][12]={{101, 102, 0, 103, 0, 0, 0, 0, 0, 0, 0, 0},{104, 105, 106, 107, 108, 0, 0, 0, 0, 0, 0, 0},{0,109,110,0,111,112,0,0,0,0,0,0},{113,114,0,115,116,0,117,0,0,0,0,0},{0,118,119,120,121,122,123,124,0,0,0,0},{0,0,125,0,126,127,0,128,129,0,0,0},{0,0,0,130,131,0,132,133,0,134,0,0},{0,0,0,0,135,136,137,138,139,140,141,0},{0,0,0,0,0,142,0,143,144,0,145,146},{0,0,0,0,0,0,147,148,0,149,150,0},{0,0,0,0,0,0,0,151,152,153,154,155},{0,0,0,0,0,0,0,0,156,0,157,158}};
	
	
	double *Ac;
	int *Ic, *Jc;

	/*Allocate dynamic memory */
		Ac = malloc((size_t) ((nz+1)*sizeof(double)) );
		Jc = malloc((size_t) ((nz+1)*sizeof(int)) );
		Ic = malloc((size_t) ((13)*sizeof(int)) );
		/*Jc = ivector(1,nz+1);

	/*Call CSR() */
		CSR(A, 12, 12, &nz, Ac, Ic, Jc, 0);
		
	
		for(i=0; i<=nz-1; i++)
			printf("Ac[%d] = %.1f\t Jc[%d] = %d\n", i, Ac[i], i, Jc[i]);
		
		for(i=0; i<=12; i++)
			printf("Ic[%d] = %d\n", i, Ic[i]);		
		
		
	/*Free dynamic memory*/
	free(Ac);
	free(Jc);
	free(Ic);
	
return;
}
CSR()
Code:
#include<stdio.h> 
#include<stdlib.h>

void CSR(double A[][12], int nrow, int ncol, int *nz, double *Ac, int *Ic, int *Jc, int array_numb)
{
 /*	array_numb:put 0 if your array is allocated in a 0-type style (n elementes from 0 to n-1)
  *			    put 1 if your array is allocated in a 1-type style (n elementes from 1 to n)
  */

	int i,j,k;
	int _nz;
	
	/*count nz: nonzero elements*/
	_nz = 0;
		for(i=array_numb; i<=nrow-1+array_numb; i++){
			for(j=array_numb; j<=ncol-1+array_numb; j++){
				if( A[i][j] != 0.0 ){
					_nz++;
					Ac = realloc(Ac, (size_t) _nz * sizeof(double));
					Jc = realloc(Jc, (size_t) _nz * sizeof(int));
					
					Ac[_nz-1] = A[i][j];
					Jc[_nz-1] = j+1;
					
					Ic[i+1] = _nz+1;
					
				}
				
				
			}
		}	
		//This goes here because of the point where _nz is updated inside the loop:
		Ic[0] = Jc[0];
		
		
		for(i=0; i<=_nz-1; i++)
			printf("Ac[%d] = %.1f\t Jc[%d] = %d\n", i, Ac[i], i, Jc[i]);
		
		for(i=0; i<=12; i++)
			printf("Ic[%d] = %d\n", i, Ic[i]);
		
		*nz = _nz;
	
return;
}