Struct Arrays and Functions

Hi there,

I've been having problems trying to pass a struct pointer array in dynamic memory to a function. Could anyone please help me with where i've gone wrong?

I've cut out chunks of code that (hopefully) don't relate to the function.

I keep getting this error message:
error C2664: 'QuickSort' : cannot convert parameter 1 from 'person **' to 'person **[]'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I've tried different combinations of []'s but no luck. Can anyone help with what i'm doing wrong? Would be much appreciated.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//Before main
void QuickSort(person** arr[], int left, int right);

//In main
person** pointerset = new person*[FL];

//(more code)...
QuickSort(pointerset, 0, FL - 1);  

//After main
void QuickSort(person** arr[], int left, int right) {

      int i = left, j = right;

      person** tmp;
//pivot is abritrary middle value
      int pivot = (**(arr[(left + right) / 2])).mobile;
	  //

 

      /* partition */

      while (i <= j) {

            while ((**(arr[i])).mobile < pivot)

                  i++;

            while ((**(arr[j])).mobile > pivot)

                  j--;

            if (i <= j) {

                  tmp = arr[i];

                  arr[i] = arr[j];

                  arr[j] = tmp;

                  i++;

                  j--;

            }

      };

 

      /* recursion */

      if (left < j)

            QuickSort(arr, left, j);

      if (i < right)

            QuickSort(arr, i, right);

}

//Quicksort taken and adapted from http://www.algolist.net/Algorithms/Sorting/Quicksort



I assume you are passing pointerset, which would explain your error. In an argument list, [] and * are used interchangeably. Therefore, in your arg list you want *** (which you represent as **[]) and you are passing a **. These are two different levels of pointerness (or dereferenceability, whatever you want to call it) so the call is in error.
EDIT: You may want to check the tutorial for arrays and passing them to functions, on this website. Passing a * is the same as passing a [].
Last edited on
ah yeh... i kept presuming it was wrong because i got even more errors relating to the pointers inside the function, but it was those that needed editing after getting rid of the [].

many thanks
Topic archived. No new replies allowed.