Click here to Skip to main content
15,305,969 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm pretty sure it's a simple error in the arithmetic but I just cant wrap my head around it..I keep getting this error: invalid operand to binary +(have int* and int*).

C++
void sort_ver2(int *pointer, int size)
{
    int *i, *j;
    int temp;
    for(i = pointer; i < (pointer+size); ++i){
        for(j = pointer + 1; j < (pointer+size); ++j){
            if((pointer + i) < (pointer + j)){
                temp=(pointer + j);
                (pointer + j)=(pointer + i);
                (pointer + i)=temp;

            }
        }
    }


}


Thanks in advance!
Posted
Updated 20-Oct-13 2:52am
v2

Have a close look:
C#
for(i = pointer; i <; (pointer+size); ++i){
                    ^
Should this be here? Or did you mean "="?



"Sorry :) updated the post, what i meant was simply "<" the colon isnt't supposed to be there."

Looking at the new version, I'm not exactly sure what you are trying to do, but I can understand why the compiler is complaining:
C++
int *i, *j;
int temp;
...
temp=(pointer + j);
Both pointer and j are pointer-to-integer variables - and you can't add two pointers, because there is nothing sensible you get! It's a bit like trying to add "East" and "Left" - it doesn't leave you anything useful as a result.

Since this is a sort function, I would suspect you would be better off writing it with i and j as int rather than int* and using them to add to your base pointer value.
And also using *(pointer+i) to access the values would probably help! :laugh:
   
v2
Comments
[no name] 20-Oct-13 8:53am
   
Sorry :) updated the post, what i meant was simply "<" the colon isnt't supposed to be there.
OriginalGriff 20-Oct-13 9:22am
   
Answer updated
The problem I believe, appears to stem from your understanding and use of pointers.
Your code pointer + i really doesn't make any sense. I'll try to illustrate with a simple diagram.

Imagine you had an array with some data in it. We'll assume that *pointer holds the address of the first element. We'll also assume an arbitrary location in memory of 0x4003000.

SQL
----------------------
| a | b | c | d | \0 |
----------------------


So,
'a' is at 0x4003000,
'b' is at 0x4003001,
'c' is at 0x4003002,
'd' is at 0x4003003
and the
NULL terminator is at 0x4003004

So, now we have that in place, lets run through the 3 lines from and including your first for loop - assuming size to be 4.

C#
for(i = pointer; i < (pointer+size); ++i){
        for(j = pointer + 1; j < (pointer+size); ++j){
            if((pointer + i) < (pointer + j)){


This will turn into

C++
for (i=0x4003000; i<0x4003000+4; ++i)
{
   for (j=0x4003001; j<0x4003000+4; ++j)
   {
       if ((0x4003000+0x4003000) < (0x4003000+0x4003001))


Starting to see the problem yet?!

The solution is, I think: (your intention isn't 1000% clear to me)
C++
void sort_ver2(int *pointer, int size)
{
    int *i, *j;
    int temp;
    for(i = pointer; i < (pointer+size); ++i)
    {
        for(j = pointer + 1; j < (pointer+size); ++j)
        {
            if( *i < *j)
            {
                temp = *j;
                *j = *i;
                *i = temp;
            }
        }
    }
}


I assume you'd like to compare the _values_ that i and j point to, swapping them if a condition is met?
A truly horrid (but perfectly valid & legal) way to use pointers, in my humble opinion. :)
   
The reason is C++ does not allow pointer addition.

What do you expect by adding pointers ? A pointer is a memory address. Adding 2 pointers will point to (maybe) another memory address in an entire different location. Since this is no use, C++ prohibits pointer addition.

I think you are confused about adding integers to pointers. The following code is valid in C++, and makes sense.
C++
int arr[5] = {1, 2, 3, 4, 5};
int* p = &arr;
int* p_third = (p + 3);


arr is an array of 5 integers. Now, p points to the array's first element(arr[0]). And p_third pointer is assigned to a value of (p + 3). So what's (p+3) ?

In C++, the last line of the above code can be also written like this:
C++
int* p_third = (p + 3 * sizeof(int));


So, (p + 3) is really (p + 3 * sizeof(int)).

At last, p_third points to arr[3].


Tip: You should stop coding like Java people do. Code like this (It's more readable and comprehensive) :
C++
if()
{
   if()
   {
      if()
      {
      }
   }
   else
   {
   }
}

   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900