my function get two exist array(+sizes) and pointer that in function build new array and realloc the size at the end. finally the function return the new size ,but the printf of new array is crash. the algorithm working casue the new size really true.
int UniteArray(int *a,int sizeA,int *b,int sizeB,int *tempArray)
{
int i=0,j=0,counter=0;
tempArray=(int*)malloc(sizeA*sizeof(int));
while (i<sizeA)
{
if (a[i]==b[j])
{
tempArray[counter++]=a[i];
i++;j++;
}
else if (a[i]<b[j])
i++;
else if (b[j]<a[i])
j++;
}
tempArray=(int*)realloc(tempArray,counter);
return counter;
}
here the function call;
void Ex3()
{
int *a,*b,*unite;
int sizeA,sizeB,newSize,counter,i;
a=BuildArray(&sizeA);//Build the A array.
b=BuildArray(&sizeB); //Build the B array.
merge_sort(a,0,sizeA-1);//merge the A array.
merge_sort(b,0,sizeB-1); //merge the B array.
counter=UniteArray(a,sizeA,b,sizeB,&unite); //<<<<<<< HERE
printf ("The new Length: %d.\n",counter);
for (i=0;i<counter;i++)
printf ("%d ",unite[i]);