448,781 Members | 1,470 Online
Need help? Post your question and get tips & solutions from a community of 448,781 IT Pros & Developers. It's quick & easy.

function that uses size of array??

 P: n/a 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; Apr 26 '07 #1
5 Replies

 P: n/a 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? For an array, they do. For a std::list, they don't. The function would work for a std::vector. 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: If elements do not come in line, how do you know that *(++pFirst) will point to the second element? It's prefectly OK to use this for arrays. However, since we are 2007 and this is C++, please _do_ use std::vector instead. Apr 26 '07 #2

 P: n/a Gernot Frisch wrote: >I have been told that elements in an array does not necessary lieafter each other in memory. So if I wanted a char array or stringarray instead I guess the above function would not work? For an array, they do. For a std::list, they don't. The function would work for a std::vector. >In that case how do I make sure that I access the right elements inthe right order in an array and that I can calculate its size with: If elements do not come in line, how do you know that *(++pFirst) will point to the second element? It's prefectly OK to use this for arrays. However, since we are 2007 and this is C++, please _do_ use std::vector instead. ok but will I get the correct size of a std::list with integers if I use: int* myfind(int* arr_start, int* arr_end, int& s) { int arr_size = (arr_end - arr_start)+1; does arr_size not only give the right result if elements are placed after each other in memory? Apr 26 '07 #3

 P: n/a desktop wrote: :: Gernot Frisch wrote: :::: 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? ::: ::: For an array, they do. For a std::list, they don't. ::: The function would work for a std::vector. ::: :::: 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: ::: ::: If elements do not come in line, how do you know that *(++pFirst) ::: will point to the second element? ::: ::: It's prefectly OK to use this for arrays. However, since we are 2007 ::: and this is C++, please _do_ use std::vector instead. ::: ::: :: :: ok but will I get the correct size of a std::list with integers if I :: use: :: :: :: int* myfind(int* arr_start, int* arr_end, int& s) { :: int arr_size = (arr_end - arr_start)+1; :: :: :: :: does arr_size not only give the right result if elements are placed :: after each other in memory? Yes, but with std::list and std::vector you don't have to compute the size yourself, just call the container's size() member function. Also, you might not need the size at all, as the containers also provide begin() and end() functions! Bo Persson Apr 26 '07 #4

 P: n/a On Apr 26, 10:52 am, desktop template< typename T > T* find_ptr(T* const pbegin, T* const pend, const T& t) { size_t size = pend - pbegin; std::cout << "size = " << size; std::cout << std::endl; for(T* p = pbegin; p != pend; ++p) { if(t == *p) return p; } return pend; } int main() { int nums[] = { 1, 2, 3, 4, 5, 7, 8, 9 }; // p_end is one past the end of container int* const p_end = &nums[ sizeof(nums)/sizeof(int) ]; int* p_result = find_ptr(&nums[0], p_end, 7); // if p_end wasn't returned, something was found if( p_end != p_result ) { std::cout << "found at: " << p_result; std::cout << "\tvalue: " << *p_result; std::cout << std::endl; } else { std::cout << "p_end reached, element not found.\n"; } } /* size = 8 found at: 0x7fff7ef0e554 value: 7 */ /* another way... pass the whole array by reference template< typename T, const size_t Size > T* find_ref(T (& array)[ Size ], const T& t) { size_t size = sizeof(array)/sizeof(T); std::cout << "size = " << size; std::cout << std::endl; for(size_t i = 0; i < size; ++i) { if(t == array[i]) return &array[i]; } // one past the last element return &array[Size]; } */ Apr 26 '07 #5

 P: n/a On Thu, 26 Apr 2007 16:52:03 +0200, desktop