0

Suppose you have passed a pointer to some kth element in the array and you wish to search through that array. How can I make that pointer point to the beginning of the array? Here is the pseudocode of what I mean:

main(){
    int arr[5]{1,2,3,4,5};
    subRoutine(arr+2); //
}

void subRourtine(int * ptr){
    while(ptr){
        ++ptr; //This only work if i'm looking for an element that is after ptr
    }
}
mr5
  • 3,438
  • 3
  • 40
  • 57
Bobby
  • 496
  • 5
  • 18
  • Possible duplicate of [c++ how to get the length of a pointer array?](http://stackoverflow.com/questions/15774534/c-how-to-get-the-length-of-a-pointer-array) – user657267 Dec 09 '15 at 00:18
  • 1
    You cannot know where the beginning of the array is when you only have a pointer to somewhere inside it, unless the array data itself contains a sentinel value at the beginning. It's better to pass a pointer _and_ an offset (and a size). – paddy Dec 09 '15 at 00:18
  • Why not also pass a `int * end_ptr`? – Brian Rodriguez Dec 09 '15 at 00:19
  • i know its better to pass a length but i can't add parameters for multiple reasons – Bobby Dec 09 '15 at 00:21
  • note that `while(ptr) ++ ptr;` is incorrect - you just run off the end of the array – M.M Dec 09 '15 at 00:45
  • what i meant by that is while the pointer is not `NULL` increment it, is how it's usually done? – Bobby Dec 09 '15 at 00:58
  • @Bobby When does it become null if you only increment it? – Emil Laine Dec 09 '15 at 01:18
  • @zenith you are right, the pointer will just point anyway when it get pass the array, perhaps `while(*ptr)` Will solve the problem – Bobby Dec 09 '15 at 01:35
  • No, it won't. Because in that case you just run off the end of the array and get Undefined Behaviour. Chances are that if you don't crash, you might just be lucky and have a zero on the stack following your array. If I saw someone doing this kind of thing in a code review, I'd have them removed from my team. – paddy Dec 09 '15 at 02:06

1 Answers1

3

Pointers support normal arithmetic, so if you know the value of k involved, you can just subtract that value to get back to the beginning of the array.

If you don't know the value of k, you might be able to get by if you know know unique value that signals the beginning of the array.

If you don't know either of those, you're pretty much out of luck. C++ doesn't provide a way to find the beginning of an array given only a pointer to some arbitrary point inside that array.

As an aside, note that the code you've give using while (ptr) doesn't really work for traversing to the end of the array either. It tries to keep going until incrementing the point gives a null pointer, which might never happen--and even if it does, there's a pretty good chance of the pointer going well past the end of the array first anyway.

Given the situation you describe, I'd consider (if at all possible) passing the array itself rather than a pointer:

template <class T, size_t N>
void subRoutine(T (&array)[N]) { 
    for (int i=0; i<N; i++)
        process(array[i]);
}

You'd invoke this something like: subRoutine(arr); The compiler will deduce the size of arr for you, so you don't need to pass an extra parameter to specify the size.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • So what will be best way to traverse till the end of the array in that case, isn't that the only way? – Bobby Dec 09 '15 at 00:26