D
desktop
I have a function that takes two pointers to an array. The first point
to the first element while the other points to the last element.
int nums[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* result;
int end = (sizeof(nums)/4)-1;
int s = 7;
result = ::myfind(nums, nums + end, s);
the implementation goes like:
int* myfind(int* arr_start, int* arr_end, int& s) {
int arr_size = (arr_end - arr_start)+1;
int* result = arr_end;
for (int i = 0; i < arr_size; i++) {
if (*(arr_start+i) == s)
{
result = arr_start+i;
}
}
return result;
}
I have been told that elements in an array does not necessary lie after
each other in memory. So if I wanted a char array or string array
instead I guess the above function would not work?
In that case how do I make sure that I access the right elements in the
right order in an array and that I can calculate its size with:
int arr_size = (arr_end - arr_start)+1;
to the first element while the other points to the last element.
int nums[] = { 1, 2, 3, 4, 5, 7, 8, 9};
int* result;
int end = (sizeof(nums)/4)-1;
int s = 7;
result = ::myfind(nums, nums + end, s);
the implementation goes like:
int* myfind(int* arr_start, int* arr_end, int& s) {
int arr_size = (arr_end - arr_start)+1;
int* result = arr_end;
for (int i = 0; i < arr_size; i++) {
if (*(arr_start+i) == s)
{
result = arr_start+i;
}
}
return result;
}
I have been told that elements in an array does not necessary lie after
each other in memory. So if I wanted a char array or string array
instead I guess the above function would not work?
In that case how do I make sure that I access the right elements in the
right order in an array and that I can calculate its size with:
int arr_size = (arr_end - arr_start)+1;