+4 votes
by (13.6m points)

I'm trying to have some practice with C using Xcode, but got stock with some error.

The code is as follows:

#include <stdio.h>
#include <stdlib.h>

/* move previous elements down until insertion point reached */
void shift_element (unsigned int i ) {
    int ivalue;
    // guard against going outside array
    for (ivalue = arr[i]; i && arr[i-1] > ivalue; i--) {
        arr[i] =arr[i-1];   // move element down
    arr[i] = ivalue; // insert element
    }
}

int main(int argc, const char * argv[]) {

    unsigned int arr[5] = {2,4,5,3,6};
    shift_element(3);

    // print arr
    int i;
    for (i=0;i < (sizeof (arr) /sizeof (arr[0]));i++) {
        printf("%d\n",arr[i]);
    }
    return 0;
}

But it results in an error saying 'use of undeclared identifier 'arr''

I searched previous questions with the same subject but could not find a proper answer that would solve my problem.

I would be so happy if somebody can help.

Cheers

4 Answers

+2 votes
by (13.6m points)

The arr variable isn't in scope of the shift function. You have to pass it in as a parameter.

0 votes
by (13.6m points)

You forgot to declare your 'arr'.

Here is a very good answer explaining the various way do to that : How to initialize all members of an array to the same value?

EDIT : Didn't see you declared it in the function below. As the other answer says, you have to pass it as a parameter tho.

0 votes
by (13.6m points)

Your problem lies in variable accessibility. When you define a variable in the main function it is only accessible in the function in which it was defined. You to try access the array in the shift_elements function which is causing your issue.

To fix this you can either make the arr variable global or pass it in as an argument to your shift_elements(function). I coded a quick correction doing the later because its good practice to avoid global variables when possible.

#include <stdio.h>
#include <stdlib.h>

/* move previous elements down until insertion point reached */
void shift_element (unsigned int i, int[] arr) {
    int ivalue;
    // guard against going outside array
    for (ivalue = arr[i]; i && arr[i-1] > ivalue; i--) {
        arr[i] =arr[i-1];   // move element down
    arr[i] = ivalue; // insert element
    }
}

int main(int argc, const char * argv[]) {

    unsigned int arr[5] = {2,4,5,3,6};
    shift_element(3, arr);

    // print arr
    int i;
    for (i=0;i < (sizeof (arr) /sizeof (arr[0]));i++) {
        printf("%d\n",arr[i]);
    }
    return 0;
}

For more information on types of variables look here: http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/1-C-intro/kinds-vars.html

0 votes
by (13.6m points)

Just to share with those future possible readers, the correct code would be:

#include <stdio.h>
#include <stdlib.h>

/* move previous elements down until insertion point reached */
void shift_element (unsigned int i, unsigned int *arr ) {
    int ivalue;
    // guard against going outside array
    for (ivalue = arr[i]; i && arr[i-1] > ivalue; i--)
        arr[i] = arr[i-1];  // move element down
    arr[i] = ivalue; // insert element

}

int main(int argc, const char * argv[]) {

    unsigned int arr[5] = {2,4,5,3,6};
    // print arr
    int i;
    for (i=0;i < (sizeof (arr) /sizeof (arr[0]));i++) {
        printf("%d\n",arr[i]);
    }

    shift_element(3, arr);

    // print arr

    for (i=0;i < (sizeof (arr) /sizeof (arr[0]));i++) {
        printf("%d\n",arr[i]);
    }
    return 0;
}

Related questions

...