need help with passing an array in a recursive (merge sort) function

my assignment is to get a function to merge sort data. we can use either arrays or linked lists as our data structure, and i picked arrays. here is my function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void merge_sort(int *list[], int listlength)
{
      //if the list only has one item, return the list to the previous function
      if (listlength<=1)
      {
         return;
      }
      
      
      //initalize the lists that are going to be used
      int left[listlength/2];
      int leftlength = listlength/2;
      int right[listlength-listlength/2];
      int rightlength = listlength-listlength/2;
      int result[listlength];
      
      
      //split the list into two halves to be merge_sorted
      for (int i=0; i<=listlength/2; i++)
      {
          left [i]= *list [i];
      }
      int j=0;
      for (int i=listlength/2+1; i<=listlength; i++)
      {
          right [j]= *list [i];
          j++;
      }
      
      
      //here we merge_sort each half
      merge_sort(*left[], leftlength);
      merge_sort(&right[], rightlength);
      
      
      //then merge the two sides together!
      merge(left[], right[], &result[], leftlength, rightlength);
}


for my last 3 lines of code, for the parts where there is '[]', it says "expected primary-expression before ']' token"

if i take out the brackets, it says: "cannot convert `int (*)[((unsigned int)((int)(listlength / 2)))]' to `int**' for argument `1' to `void merge_sort(int**, int)'"

can anyone please help me with this? i feel like i'm so close right now.
Last edited on
Why does this function take an array of int* types?
what do you mean? i thought i had to have the * in there to make it pass by reference? to be honest i am not so sure if i am using all these pointers and what not correctly. i hate using pointers.
i was trying to use this example from wikipedia:
http://en.wikipedia.org/wiki/Pass-by-reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Modify(int p, int * q, int * o)
{
    p = 27; // passed by value - only the local parameter is modified
    *q = 27; // passed by value or reference, check call site to determine which
    *o = 27; // passed by value or reference, check call site to determine which
}
int main()
{
    int a = 1;
    int b = 1;
    int x = 1;
    int * c = &x;
    Modify(a, &b, c);   // a is passed by value, b is passed by reference by creating a pointer,
                        // c is a pointer passed by value
    // b and x are changed
    return(0);
}
can anyone help?
In your function header, you have

void merge_sort(int *list[], int listlength)

The first argument means a pointer to an array. Loosely speaking (someone else will have to explain to you the difference...), it is similar to: int **list, or an array of int* types.

When you pass an array to a function, you are already passing a pointer -- you're passing a pointer to the beginning of the array. So, you don't need the extra *.

Personally, I still get all the * and [] confused from time to time, especially when I'm lacking sleep. :-) Try to step back from mergesort and see if you can create an array of integers in one function and pass it to another function to print it out...

finally got it! got rid of all the *'s and &'s and it ran but didn't work, then i just had to change "result" in line 37 to "list" so it would actually effect the damn list.
Topic archived. No new replies allowed.