We’ve written the following recursive function in my C++ class.
bool contains(int arr[], int n, int val){
if(n == 0) return false; // Escape clause
if(arr[0] == val) return true; // Second escape clause
return contains(arr+1, n-1, val);
}
My professor said that C/C++/Assembler are the only mainstream languages which support this kind of array traversal. What other languages allow for this kind of search by changing the pointer address?
All languages which support numeric indexes on arrays would work for this general algorithm. It has nothing to do with pointer manipulation.
Pointer manipulation is usually taken advantage of for string based algorithms in C. For example it’s possible to implement
strlenas follows.This code would work equally well on a
char[]orchar*input. This type of manipulation of pointer is indeed limited to languages which both expose pointers and expose arrays in that way.This isn’t limited to C / C++ / Assembler though. It could also be done in say C#