How to construct an temporary array in a function?

I am trying to write a merge sort function using divide-and-conquer method like this:

1
2
3
4
void mergeSort(int *a)
{
//...
}


In this function, I have to construct another array, which has the same length of array a. But I don't know how to do it.

1
2
3
4
void mergeSort(int *a)
{
 int* b= // I don't know how to construct the array b with the same length of a.
}


Does anyone know how to do it? Many thanks!
i'm not great with C, but don't you normally pass in the size of the array as the second parameter to methods like this?
You'd have to. There's no way to find out the size of array a otherwise.
There is a way to find the size of the array using the sizeof operator:

size_t size_a = (sizeof a / sizeof *a);

The division is required because the sizeof operator returns the number of bytes. So dividing by the size of an individual element will give you the number of elements in the array

http://ideone.com/UuB91g
Last edited on
Smac89 wrote:
There is a way to find the size of the array using the sizeof operator:

That only works when the type of the identifier is an array (or a reference to an array.) Here, the type of the variable with identifier a is a pointer, not an array.

http://ideone.com/t7tdwx
Last edited on
Topic archived. No new replies allowed.