-3

I'm writing some code in C++ to do the quick sort algorithm to sort an array of integers. I have my complete code here:

void swap(int A[], int x, int y)
{
    int tmp;
    tmp=A[x];
    A[x]=A[y];
    A[y]=tmp;
}

int partition(int A[], int start, int stop)
{
    int big=start;
    int pivot=A[stop];
    for(int i=start; i<stop; i++)
    {
        if(A[i]<=pivot)
        {
            swap(A, i, big);
            big++;
        }
    }
    swap(A, big, stop);
    return big;
}


void quick_Sort_Helper(int A[], int start, int stop)
{
    //base cases
    if(stop<=start)
    {return;}

    if(start+1==stop)
{
    if(A[start]>A[stop])
    {swap(A, start, stop);}
    return;
}

//recursive cases
int pivot=partition(A,start, stop);
quick_Sort_Helper(A,start, pivot-1);
quick_Sort_Helper(A, pivot, stop);
}

void quick_Sort(int A[])
{
    quick_Sort_Helper(A,0, A.size()-1);
}

I get an error when compiling the program on the line where I call A.size(). The error reads as follows:

error: request for member ‘size’ in ‘A’, which is of non-class type ‘int*’

I don't understand because the size() function of an array is supposed to return an integer with the length/ number of elements in the array.

Megumi Ai
  • 89
  • 9
  • 1
    No, arrays do not have a `size()` function. They never did. Never will. This is C++, not Java. – Sam Varshavchik Nov 16 '16 at 01:13
  • 2
    > I don't understand because the size() function of an array is supposed to return an integer with the length/ number of elements in the array. Not sure where you got that.... – mascoj Nov 16 '16 at 01:13
  • I don't see why all the downvotes, seems like a reasonable question to me, with enough detail, and probably not the most easy thing to search for on Google. – Jonathan Nov 16 '16 at 01:19
  • 2
    "array size C++" turns up [How do I find the length of an array?](http://stackoverflow.com/questions/4108313/how-do-i-find-the-length-of-an-array) near the top of the first page of Google, Bing, Yahoo, and DuckDuckGo. – user4581301 Nov 16 '16 at 01:33
  • @user4581301 yeah, if you knew that is what the problem was. It can be somewhat difficult to gather that from the "gibberish compile error". – Jonathan Nov 16 '16 at 01:46
  • 1
    @Jonathan "Array size C++" was chosen because OP was having trouble with a line of code that was trying to get the *size* of an *array* in *C++*. I'm right with you on the uselessness of a websearch when you don't know the words to search for but in this case, pasting the error message verbatum into google popped up the following SO question: [“error: request for member ‘size’ in ‘a’, which is of pointer type” but I didn't think it was a pointer](http://stackoverflow.com/questions/22921998/error-request-for-member-size-in-a-which-is-of-pointer-type-but-i-didnt) which is a near-identical dupe. – user4581301 Nov 16 '16 at 05:53

4 Answers4

2

Raw arrays are pretty dumb. They are a blocks of memory without any support methods. Further, when you pass them around they tend to decay to pointers losing what little meta information they did have. If you need to preserve this information consider using std::vector (resizable) or std::array (static size).

If these are not available for whatever reason, consider writing a simple wrapping structure to contain the sizing information with the array and pass the structure around.

Community
  • 1
  • 1
user4581301
  • 29,019
  • 5
  • 26
  • 45
1

Arrays do not have methods. So this expression A.size()-1 is invalid. You have to pass explicitly to the function the number of elements in the array. Thus the function declaration will look like

void quick_Sort( int A[], size_t n );

where n is the number of elements in the array A.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
1

Array in C++, unlike Java, is not a class. If you want to use Arrays as a class, you could use array class.

array<int,5> A;

Reference to Array class C++


You can't directly pass array into a function in c++, because arrays are second class citizens and they decay into a pointer. If you still want to pass an array, there is no option but to pass the size as a parameter.


If you declared an array in the same scope as asking for it's size, you can do this,

int l = sizeof(A)/sizeof(A[0]);
Tarun Maganti
  • 2,274
  • 2
  • 22
  • 53
1

In C++, an array does not come with any functionality whatsoever; it is quite literally a block of contiguous memory just large enough to hold whatever data it is declared to hold (in your case it is sizeof(int) * numberOfItems).

I suggest moving to use std::vector instead (in your case this would be std::vector< int >), which under the hood does manage an array, but provides the niceties of size() function, iterators, random access, automatic resizing, etc.

Alternatively, if you are going to use arrays directly like this, you need to pass along its length to any function that is going to operate on the array. Otherwise, the function has absolutely no way of knowing the size of the array.

Nicholas Smith
  • 780
  • 6
  • 16