3

I was helping my friend with an assignment and it has been a while since I've used C so I cannot find why this works but the first code works and the second one doesn't. This isn't for the assignment since we figured it out but I want to know why this is happening.

void GnomeSort( int gnomes[], int length ) {

    int pos = 1;

    while ( pos < length ) {

        if ( gnomes[ pos ] >= gnomes[ pos - 1 ] ) {

            pos = pos + 1;
        }
        else {

            swap( gnomes + pos, gnomes + pos - 1 );
            if ( pos > 1 ) {

                pos = pos - 1;
            }
        }
    }
    return;
}

This doesn't work:

void GnomeSort( int gnomes[], int length ) {

    int pos = 1;

    while ( pos < length ) {

        if ( gnomes[ pos ] >= gnomes[ pos - 1 ] ) {

            pos = pos + 1;
        }
        else {

            swap( gnomes[ pos ], gnomes[ pos - 1 ] );
            if ( pos > 1 ) {

                pos = pos - 1;
            }
        }
    }
    return;
}

The difference is the swap call:

swap( gnomes + pos, gnomes + pos - 1 );

as opposed to:

swap( gnomes[ pos ], gnomes[ pos - 1 ] );

Here is the Swap function that could not be changed:

void swap(int* ptrA, int* ptrB) {
    int temp = *ptrA;
    *ptrA = *ptrB;
    *ptrB = temp;
    return;
}

It is obviously in the input for the swap function, but is it this way? What other ways can you make the input into the swap function?

Dai
  • 141,631
  • 28
  • 261
  • 374
F22lightning
  • 620
  • 6
  • 16

4 Answers4

6

It's because gnomes + pos is not equivalent to gnomes[pos].

  • gnomes + pos will return a pointer value. This is because a pointerType + integerType => pointerType.

  • gnomes[pos] is actually *(gnomes+pos), so you end-up passing in a dereferenced pointer value, an int in your case, to the swap function.

Your compiler should give you an error, or at least a warning, about that.

Dai
  • 141,631
  • 28
  • 261
  • 374
2

swap function accepts two pointers as its arguments int* ptrA, int* ptrB , so while calling swap function, u need to pass by reference (address of gnomes)

gnomes + pos gives you the address while gnomes[ pos ] gives you the value at that address

Hardik Sanghvi
  • 416
  • 4
  • 12
1

It's because swap() takes two pointers to integers as its input, not the integers themselves. When you use gnomes[ pos ] and gnomes[ pos - 1 ] you are passing integers into the swap function, but that's not what it expects. When you use gnomes + pos and gnomes + pos - 1 you are passing pointers to integers.

Arithmetic operations on pointers simply move the pointer forward or backwards in memory.

medman826
  • 92
  • 7
0

The reason for this is C is pass by value.

When you give your parameters, gnomes[pos], gnomes[pos-1], swap gets a copy of these ints. Any changes inside the swap function on the ints are not visible to the caller.

You can however change the value of your parameters when you pass a pointer ie: gnomes+pos, gnomes+pos-1

Here is a good post with some more details on pass by value

What's the difference between passing by reference vs. passing by value?

Community
  • 1
  • 1
Luca
  • 13
  • 4