• Source
    1. #include <iostream>
    2.  
    3. void* my_find(void *key, void *array, int ElementSize, int n)
    4. {
    5. bool match = false;
    6. char *KeyCOPY = (char*)key;
    7. char *ArrayCOPY = (char*)array;
    8. for (int i = 0; i < n; i++)
    9. {
    10. for (int Element = 0; Element < ElementSize; Element++)
    11. {
    12. if (*KeyCOPY != *ArrayCOPY)
    13. {
    14. match = false;
    15. break;
    16. }
    17. else {
    18. match = true;
    19. }
    20. KeyCOPY++;
    21. ArrayCOPY++;
    22. }
    23. KeyCOPY = (char*)key; // BUG: What about ArrayCOPY in the case of false match???
    24. if (match == true)
    25. {
    26. for (int i = 0; i < ElementSize; i++)
    27. {
    28. ArrayCOPY--;
    29. }
    30. return ArrayCOPY;
    31. }
    32. }
    33. return NULL;
    34. }
    35.  
    36. char TAB[] = { 'A','B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
    37.  
    38. int main()
    39. {
    40. void* tab = TAB;
    41.  
    42. char x = 'D';
    43.  
    44. std::cout << my_find(&x, TAB, sizeof(char), 10); // Wrong answer
    45. return 0;
    46. }
    47.  
    48.