1

I am using an insertion sort function to sort a 5000 int ascending array. when i pass the array's address to the function call, i get [Error] name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]. Maybe I should use a vector but I am unfamiliar with them. Maybe I coded something wrong?

#include <iostream>
#define SIZE 5000 //array size
using namespace std;

void insertionSort(int arr[], int length) {
      int i, j, tmp;
      for (i = 1; i < length; i++) {
            j = i;
            while (j > 0 && arr[j - 1] > arr[j]) {
                  tmp = arr[j];
                  arr[j] = arr[j - 1];
                  arr[j - 1] = tmp;
                  j--;
            }
      }
}

int main() {
    int a[SIZE];
    int count = 1;

    for(int i=0; i<SIZE; i++) {
        a[i] = count;
        count++;
    }

    for(int i = 0; i < SIZE; i++) {
        printf("%d\t", a[i]);
    }

    insertionSort(&a[i], SIZE);
}
1
  • In your main() function i is not defined when you call insertionSort(&a[i], SIZE);. This is because i only exists inside of your for loops... See @michael-schmitt answer below. Commented May 5, 2015 at 0:15

2 Answers 2

2

You probably want to call

 insertionSort(a, SIZE)
Sign up to request clarification or add additional context in comments.

2 Comments

Wow. Thank you. I thought you could only pass array arguments by reference. I dont just want a fix, could you explain why i didn't have to pass the address instead of just the array? And, thank you again for the advice.
The array identifier points to the start of the array, so it is essentially the address already.
2

// insertionSort(&a[i], SIZE); Here you haven't declared i in funciton main(), only in your for loop.
And the call to insertionSort is outside of your loop.

Passing (&a)[0] and a mean the same thing.

2 Comments

Thank you all for your time.
'Passing (&a)[0] and a mean the same thing.' – that's wrong. Should be: 'Passing &(a[0]) and a mean the same thing.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.