と私はちょうどそれがうまくいけない理由を理解できません。 は、ここに私の関数である。私は2つの配列の交差を見つける関数を書こうとしました
int* IntersectionOfArrays(int* arr1, int size1, int* arr2, int size2, int* sizeRes)
{
int* res=(int*)malloc(1*sizeof(int)); //res is the array of the resolution of intersection//
int i = 0, j = 0;
*sizeRes = 0;
merge_sort(arr1,0, size1-1); //sorting the arrays//
merge_sort(arr2,0, size2-1);
while (i < size1 && j < size2)
{
if (arr1[i] < arr2[j])
i++;
else if (arr1[i] > arr2[j])
j++;
else
{
res[*sizeRes] = arr1[i]; //getting the same elements of the two arrays - the intersection values//
i++;
j++;
(*sizeRes)++;
res = (int*)realloc(res, 1*sizeof(int)); //allocating more memory as required - according to the size of res(intersection)//
}
}
if (*sizeRes==0) //if the intersection is empty
return NULL;
return res;
}
この関数はコンパイルが、私はそれを実行したときに動作しません。
されていませんでした。それは何をするためのものか?あなたは何をすると思いましたか? –
また、なぜ 'res'を同じサイズに保つために' realloc'を呼び出すのですか? –
'realloc()'のマンページをもう一度読むことをお勧めします。 – EOF