C++ Question regarding Illegal Indirection and Sorting by Pointer

Discussion in 'OT Technology' started by Scoob_13, Jan 24, 2003.

  1. Scoob_13

    Scoob_13 Anything is possible, but the odds are astronomica

    Joined:
    Oct 5, 2001
    Messages:
    73,796
    Likes Received:
    38
    Location:
    Fort Worth. Hooray cowgirls.
    I'm having issues with the following errors coming back during compile:

    Code:
    project1.cpp(67) : error C2100: illegal indirection
    project1.cpp(67) : error C2100: illegal indirection
    project1.cpp(69) : error C2440: '=' : cannot convert from 'int' to 'int *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    project1.cpp(71) : error C2440: '=' : cannot convert from 'int *' to 'int'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    
    Even though there should be no errors with this, seeing as it's code directly from my professor (the sorting code is directly from a handout) it doesn't work. Sorting the array normally via the bubblesort works just fine, however I have to sort the array via pointers.

    By the way, ignore the names of the variables since the actual variable names will change once I get ready to turn it in and add remarks.

    I'm only including the portion that I'm having issues with since the rest is working as it should.

    Code:
    #include <iostream.h>
    
    
    int *array(int *,int);
    float average(int *,int);
    int *sort(int *,int);
    
    
    
    /******************************************************************************/
    
    int main(void)
    {
    
    	int *arraygrds = 0, *sorted = 0, numgrades = 0;
    	float avg = 0;
    
    	cout << "Enter the number of grades" << endl;
    	cin >> numgrades;
    	arraygrds = array(arraygrds,numgrades);
    	avg = average(arraygrds,numgrades);
    	sorted = sort(arraygrds,numgrades);
    //	sort(arraygrds,numgrades);
    	for(int j=0;j<numgrades;j++)
    	{
    		cout << "Grade " << (j+1) << ": ";
    		cout << arraygrds[j] << "    Address: " << &arraygrds[j] << endl;
    	}
    	
    	cout << "The average is: " << avg << endl;
    
    
    
    	return 0;
    }
    
    /******************************************************************************/
    
    
    
    /******************************************************************************/
    
    int *sort(int *arraygrds,int numgrades)
    {
    	int *hold;
    	for(int pass=1;pass < numgrades;pass++)
    	{
    		for(int m=0;m<(numgrades-1);m++)
    		{
    			if((*arraygrds[m]) > (*arraygrds[m+1]))
    			{
    				hold = arraygrds[m];
    				arraygrds[m] = arraygrds[m+1];
    				arraygrds[m+1] = hold;
    			}
    		}
    	}
    	return arraygrds;
    }
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The problem is here:

    if ((*arraygrds[m]) > (*arraygrds[m+1]))

    *arraygrds dereferences the pointer, so the array operator [] is being applied to an int, not an int*. You need to get rid of the pointer dereference.
     
  3. Scoob_13

    Scoob_13 Anything is possible, but the odds are astronomica

    Joined:
    Oct 5, 2001
    Messages:
    73,796
    Likes Received:
    38
    Location:
    Fort Worth. Hooray cowgirls.
    Thanks for the input, that sort of helped, now I just have to figure out how to sort the damn thing :bigthumb:
     
  4. Chimpa Codigo

    Chimpa Codigo Bаnned bу Ѕuреr Modulators

    Joined:
    Jan 27, 2001
    Messages:
    68,463
    Likes Received:
    21
    Location:
    Salinas, CA
    if you're trying to sort numbers always have a swap function that will switch one address with another. I don't like doing peoples work for them, but this function will help you immensly(sp?).

    Code:
    swap(int &a, int &b)
    { 
          a=a^b;
          b=a^b;
          a=a^b;
    }
    
     

Share This Page