1

So I tried to print elements of array after some point or place but its print me another number of garbage. The code need to print 7,8,9,5. I'm sure that the problem is in the line :

for (arr=x+1; arr < arr+ n; arr++) but I don't understand what to write instead this line.

Please help me and use * or & instead [](use pointers). thanks!

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

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

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

    system("PAUSE");    
    return 0;
}

void printAfterX(int* arr, int n, int* x)
{
    if (n >= arr)
    {
        printf("not found");
    }
    else
    {
        for (arr=x+1; arr < arr+ n; arr++)
        {
            printf("%d  ",*arr);
        }
    }
}
machine_1
  • 4,266
  • 2
  • 21
  • 42
saymaname
  • 39
  • 4

5 Answers5

2

Why do you need to mix pointers and indices? You are right, in the line for (arr=x+1; arr < arr+n; arr++) there is a problem, because arr < arr + n is always true for positive n. So, this loop will never end.

Also it is not clear what does if (n >= arr) mean. Here you compare number of elements in the array and pointer on this array. It is useless. It seems that you need to compare x and arr.

I'd recommend you to use index of element to simplify your code. Try this:

void printAfterX(int* arr, int n, int x)
{    
    if (x < 0 || x+1 >= n)
    {
        printf("not found");
    }
    else
    {
        for (int i = x+1; i < n; i++)
        {
            printf("%d  ", arr[i]);
        }    
    }
}

And to call this function you need to change third parameter:

printAfterX(arr, 11, 6);

Also it is better to avoid using of system("pause") - read here: system("pause"); - Why is it wrong?

Update:

Ok, if you need to use pointers, try this:

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

    if (x < arr || x+1 > arr+n)
    {
        printf("not found");
    }
    else
    {            
        for (x = x+1; x < arr + n; x++)
        {
            printf("%d  ", *x);
        }

    }
}
Community
  • 1
  • 1
Ilya
  • 4,583
  • 4
  • 26
  • 51
1

It is obvious that arr < arr + n always holds true unless it has an overflow, so the loop won't stop before that happens.

Furthermore, by writing n >= arr, you compare an int to an int *, which definitely make no sense. To check the array bound, you should also check whether it's less than the lower bound.

Finally, I think it's better to use a simple while loop.

Here is the refined code:

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

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

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

    getchar();
    return 0;
}

void printAfterX(int *arr, int n, int *x)
{
    if (x < arr || x >= arr + n)
    {
        printf("No such element");
    }
    while(x < arr + n)
    {
        printf("%d  ", *x);
        x++;
    }
}
nalzok
  • 14,965
  • 21
  • 72
  • 139
0

The line for (arr=x+1; arr < arr+ n; arr++) is an infinite loop as arr < arr+ n is always true.

Also in line if (n >= arr) you are comparing a int with a pointer,which is not meaningful.

Modify your function like this:

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

    if (n <= x-arr) //checking it doesn't exceeds the size
    {
        printf("not found");
    }
    else
    {
        for (x++; x < arr+ n; x++)
        {
            printf("%d  ",*x);
        }

    }
}
Rajeev Singh
  • 3,292
  • 2
  • 19
  • 30
0

Try it:

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

void printAfterX(int[], int, int);

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

void printAfterX(int arr[], int n, int x)    
{    
    if (n <= arr[x])   
    {   
        printf("not found");    
    }    
    else    
    {    
        for (i=x+1; i < n; i++)    
        {    
            printf("%d  ",arr[i]);    
        }      
    }
}
nalzok
  • 14,965
  • 21
  • 72
  • 139
msc
  • 33,420
  • 29
  • 119
  • 214
0

You have to pass the number of elements as a parameter to the function to know when you have reached the end of the array.

You also can't compare a variable of type int to varibale of type int *.Try this version:

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

void printAfterX(int* arr, size_t size, size_t offset);

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

    system("PAUSE");
    return 0;
}

void printAfterX(int* arr, size_t size, size_t offset)
{
    if (offset >= size)
    {
        printf("not found");
        return;
    }
    else
    {
        while(offset < size)
        {
            printf("%d  ", *(arr + offset));
            offset++;
        }
    }
}
machine_1
  • 4,266
  • 2
  • 21
  • 42