Ma250
Ma250

Reputation: 357

Why program keeps printing trash values?

I wrote a function that takes a pointer to an index and checks if it's located within the limits of an array, the function prints the rest of the elements in that array if it's true, and if it's not, it prints "Not in range". The only parameters that this function gets are the array,the length of it and the pointer. The problem I have is that the loop that is responsible for iterating the array and printing the rest of the array also prints garbage values after the last element in the array.

Notes: you can't use any other local variables, only the parameters (array,length,pointer). You also can't use [] operator, only pointer arithmetic.

Here's my code:

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

void printAfterX(int* arr, int n, int* x);

int main()
{
    int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
    printAfterX(arr, 11, arr+6);
    return 0;
}

void printAfterX(int* arr, int n, int* x)
{
    if (x >= arr && x <= (arr + n)) //this checks if x is in the limits
    {
        x++;
        for (*arr = 0; *arr <= n; *arr++)
        {
            printf("%d ", *(arr + *x));
        }
    }
    else
    {
        printf("Not in range ");
    }
}

Upvotes: 1

Views: 164

Answers (3)

Chris Turner
Chris Turner

Reputation: 8142

Based on what you describe as the specification I think there are two problems with this bit of code:

for (*arr = 0; *arr <= n; *arr++)
{
    printf("%d ", *(arr + *x));
}

Firstly, that loop is all wrong. You're comparing the current element that arr is pointing at and seeing if it's less than the length of the array. What you should be doing is using n to limit the number of times you go around the loop. You're also making the first element of the array be 0.

Secondly, you're printing random values which could possibly be outside of the array because unless *x is 0, you're not printing some arbitrary value, possibly beyond the end of arr.

Thirdly, your specification says that you want to print everything after the element pointed to by x and you're sort of trying to print everything.

Fixing all three problems you'd end up with code that looks like.

for (x++; x<arr+n; x++)
{
  printf("%d ", *arr);
}

and as mentioned elsewhere this if (x >= arr && x <= (arr + n)) should be if (x >= arr && x < (arr + n)) as otherwise you'll be OK if x is one element beyond the length of arr

Upvotes: 0

izlin
izlin

Reputation: 2138

In your for-loop you will always print n numbers, starting from x. Also you set the first element of your array to zero and use it as iterator in for (*arr = 0; *arr <= n; *arr++). As a result you will print numbers that are outside of the range of your array. I would recommend to use another approach on the for-loop like this:

    for ( ; x != arr+n; x++)
    {
        printf("%d ", *x);
    }

Upvotes: 1

EboMike
EboMike

Reputation: 77722

Should be x < (arr + n), not x <= (arr + n). Note that there are a lot of other unchecked conditions in your code. For example, you never check whether or not the element in the array is less than n or not.

If you're not sure whether to use < or <=, you can always do a simple test case in your head: Asssume that your array has two elements, then arr + 0 is valid, arr + 1 is valid, arr + 2 is not. So your n has to be less than 2.

Upvotes: 1

Related Questions