2

I'm working on a program that modifies the data of an array using only pointers. I'm trying to return the index where array[ix]==0. However, I keep getting stuck in an infinite loop. What am I doing wrong?

int firstprime(int size, int arr[]){
  int* end = arr + size;
  int* begin = arr;
    while(begin<end){
        if(*begin==0)
            return *begin;
        begin++;
    }
    return -1;
}
8
  • 4
    Post a minimal reproducible example reproducing your problem as required here please. Commented Apr 30, 2019 at 9:56
  • The code you show doesn't by itself have any problem. What are the arguments you pass to the function? How do you call it? As mentioned, please create a minimal reproducible example (that replicates the problem) and show it to us. Commented Apr 30, 2019 at 10:08
  • @Someprogrammerdude: The code posted does not return the index. It returns the value. I think that's what the OP is having trouble with. Commented Apr 30, 2019 at 10:10
  • 1
    @Tayl Like your previous question you still seem to believe that the dereference operator gives you the index. It does not. As I said in a comment to that question, *begin is exactly equal to begin[0]. The dereference operator (unary *) gives you the value where the pointer is pointing, nothing else. Commented Apr 30, 2019 at 10:16
  • As for the infinite loop you claim you have, it's not in the function you show, ans should be posted as a separate question. Though if I should guess it's because with the code you show you return the value 0, and if you use that returned value as part of a loop condition it will lead to problems, including possible infinite loops. Commented Apr 30, 2019 at 10:22

2 Answers 2

2

You can use std::distance quite easily to get the index. More info on std::distance here

Btw, the function name is also very misleading. If the function is meant to return the index of a value in the array, then consider changing that function name, like getIndex() or find(). Just pick something more meaningful.

#include <iostream>

int firstprime(int size, int *arr)
{
    int *begin = arr;
    int *end = begin + size;
    while( begin < end )
    {
        if(*begin==0)
            return std::distance(arr, begin);
        begin++;
    }
    return -1;
}

int main()
{
    int array[10] = {1,2,3,4,0,6,7,8,9,10};
    int p = firstprime(10, array);
    std::cout<< "0 found at index: " << p <<std::endl;
}

The result is:

0 found at index: 4

Online example: https://rextester.com/KVCL75042

Sign up to request clarification or add additional context in comments.

6 Comments

Though that answer doesn't explain why the OP's code is (seemingly) stuck in an infinite loop.
@πάνταῥεῖ: I don't think it did tbh. Unless his end pointer pointed before the begin pointer.
Why not simply return std::distance(array, begin)?
@ConstantinosGlynos We probably have to wait until the OP provides a minimal reproducible example, before there can be wrtiiten a concise and solid answer.
@Someprogrammerdude: Yep! That's even simpler. I'll modify.
|
1

To get the "distance" between two pointers (in the number of elements) you can either use std::distance:

return std::distance(arr, begin);  // Return the distance between current element and the beginning of the array

You can also subtract the pointers:

return begin - arr;  // Return the distance between the current element and the beginning of the array

The "distance" returned by the above two statements will be in the number of elements, and since you take the distance from the first element it will be the index of the "current" element.

1 Comment

Using begin - arr is even simpler that using std::distance. +1

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.