Why this code returns an error?

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
  #include <iostream>

using namespace std;

template <class T>
T sortArray(T data[])
{
  int arrsize = sizeof(data)/sizeof(T);
  int x,y,temp;
  for(y=0;y<arrsize;y++)
  {
      for(x =0;x<arrsize-y-1;x++)
      {

          if(data[x]>data[x+1])
          {
              temp = data[x];
              data[x] = data[x+1];
              data[x+1] = temp;

          }
      }

  }

  return  data;

}

int main()
{
    int x;
    int arr[] = {10,7,32,65,12,6};
    int sorted[] = sortArray(arr[]);
    for(x=0;x<6;x++)
    {
        cout<<sorted[x]<<endl;
    }
}





this code returns an error of
1 - C:\Qt\Qt5.2.1\Tools\QtCreator\bin\Sorting\main.cpp:34: error: expected primary-expression before ']' token
int sorted[] = sortArray(arr[]);



how to fix this bug?
^
There are a few problems here:

#1) You can't return arrays from functions.

#2) You generally cannot use empty brackets []. The only place where it's really allowed is durring a declaration. So lines 6 and 33 are ok: Line 6 is declaring an array parameter (which is really the exact same thing as a pointer)... and line 33 is declaring an array whose size is determined by the initialization list.

However line 34 won't work. You can't use empty brackets on an array that has already been declared (arr)... nor can you create an array (sorted) with empty brackets unless you initialize it with fixed data in {curly braces}

#3) sizeof() is frequently misunderstood by beginners. You generally cannot use it to get the number of elements in an array. In particular... on line 8, 'data' is not an array, but is a pointer. So sizeof(data) is going to give you the size of a pointer, not the size of the array it points to.

#4) You are (attempting to) pass 'arr' to the sort function by pointer. This means that any changes made to 'data' inside the function will directly change the contents of 'arr'. So there is no need to return a new array.


Fixes with comments:

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
template <class T>
void sortArray(T data[], int arrsize) // return void.  pass the size as a parameter
{
  // int arrsize = sizeof(data)/sizeof(T); <- don't use sizeof
  int x,y,temp;
  for(y=0;y<arrsize;y++)
  {
      for(x =0;x<arrsize-y-1;x++)
      {

          if(data[x]>data[x+1])
          {
              temp = data[x];
              data[x] = data[x+1];
              data[x+1] = temp;

          }
      }

  }

  // return  data; <- don't return anything.  Changes made to 'data'
  //   will be fed back to the calling function, since data is passed by pointer.

}

int main()
{
    int x;
    int arr[] = {10,7,32,65,12,6};
    //int sorted[] = sortArray(arr[]);  <- get rid of 'sorted'
    sortArray( arr, 6 ); // <- don't use empty brackets
            // also pass the size of the array as a 2nd param
            // 'arr' now contains the sorted data

    for(x=0;x<6;x++)
    {
        //cout<<sorted[x]<<endl;
        cout<<arr[x]<<endl;  // <- print arr instead of 'sorted'
    }
}




EDIT:

Actually there are a few other places where empty brackets can be used. Like with delete[]. But whatever.
Last edited on
Thank you very much Disch :) i really learned a lot and fixed the bugs . you really done lot of hard work to help me :)
Topic archived. No new replies allowed.