-3

I want to Receives an array, its size, and a pointer to a specific member in memory into a function. The function will check whether the pointer points to one of the members of the array.

If so, the function will print all the elements of the array that are before the sent address - not including the pointer itself. If not, the function will print an appropriate message.

This is what I tried to do and I have a lot of warnings. How can I fix that?

void printBeforeX(int* arr, int n, int* x);
int main(void)
{
    int size = 11;
    int offset = 0;
    int arr[11] = { 4 ,8 ,6 ,2 ,1 ,3 ,5 ,7 ,8 ,9 ,5 };
    printf("Please enter an offset to search in the array’s address domain");
    scanf("%d", &offset);
    getchar();

    printBeforeX(arr, size, arr + offset);
    getchar();
    return 0;
}
void printBeforeX(int* arr, int n, int* x)
{
    if (*x > n)
    {
        printf("Index %d is out of the array range",*x);
    }
}

For example for the index 5 I'm expecting the output: : 4 8 6 2 1 and for 11 I'm expecting : false message.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 5
    You seem to have skipped over the early parts of your beginners book, tutorial or class, when it discussed `scanf` and how it works. It's right that you need a pointer, but you need a pointer to a value, not a null pointer that you dereference. Try `int offset; /* No pointer! */ scanf("%d", &offset); /* Use pointer-to operator & to get a pointer */` And that's only the start of your problems. Please take a little more time reading your books or tutorials. – Some programmer dude Mar 28 '23 at 10:30

1 Answers1

2

The only way in C language to check if the x is referencing an element of the array is to iterate through it and check for equality. It is not efficient, but it is the only way permitted by the standard. Any other comparisons invoke undefined behaviour.

void printBeforeX(int* arr, size_t n, int* x)
{
    int isInArray = 0;
    // arr points to the first element of the array
    // n is the total number of elements in the array
    // x points one element further than the last element to be printed

    for(size_t index = 0; index < n; index++)
    {
        if((arr + index) == x)
        {
            isInArray = 1;
            break;
        }
    }

    if (!isInArray)
    {
        printf("x is not referencing any element of the array\n");
        return;
    }
    
    //x and after x
    //for(size_t index = x - arr; index < n; index ++)
    //{
    //    printf("arr[%zu] (x[%zu]) = %d\n", index, index - (x - arr), arr[index]);
    //}
    for(size_t index = 0;  index < x - arr; index ++)
    {
        printf("arr[%zu] = %d\n", index,  arr[index]);
    }

}


int main(void)
{
    int arr[] = { 4 ,8 ,6 ,2};
    size_t size = sizeof(arr) / sizeof(arr[0]);

    // after x
    //for(size_t offset = 0; offset < 10; offset++)
    //{
    //    printf("offset = %zu -------------------------\n", offset);
    //    printBeforeX(arr, size, arr + offset);
    //}

    for(size_t index = 0;  index < x - arr; index ++)
    {
        printf("arr[%zu] = %d\n", index,  arr[index]);
    }

}

https://godbolt.org/z/9hYMEfrfj

0___________
  • 60,014
  • 4
  • 34
  • 74