Click here to Skip to main content
15,889,315 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Question[Sovled] Win32 C++ - Child Windows Pin
lrinish2-Nov-13 9:49
lrinish2-Nov-13 9:49 
SuggestionRe: Win32 C++ - Child Windows Pin
Richard MacCutchan2-Nov-13 21:27
mveRichard MacCutchan2-Nov-13 21:27 
AnswerRe: Win32 C++ - Child Windows Pin
pasztorpisti3-Nov-13 5:25
pasztorpisti3-Nov-13 5:25 
GeneralRe: Win32 C++ - Child Windows Pin
lrinish3-Nov-13 6:32
lrinish3-Nov-13 6:32 
GeneralRe: Win32 C++ - Child Windows Pin
pasztorpisti3-Nov-13 7:01
pasztorpisti3-Nov-13 7:01 
QuestionArithmetic operation in pointer using C Pin
shanmugarajaa31-Oct-13 20:20
shanmugarajaa31-Oct-13 20:20 
QuestionRe: Arithmetic operation in pointer using C Pin
Richard MacCutchan31-Oct-13 23:47
mveRichard MacCutchan31-Oct-13 23:47 
AnswerRe: Arithmetic operation in pointer using C Pin
Bill_Hallahan1-Nov-13 18:22
Bill_Hallahan1-Nov-13 18:22 
There is a difference between ANSI C, some other C++ compilers, and what Visual Studio allows for pointer arithmetic.

Pointer arithmetic is not allowed on all platforms, as least not allowed the way you have it. There is a ptrdiff_t macro that allows taking pointer differences on many platforms, but I'm not sure whether that is allowed in ANSI C or not. In any event, that is not what you need.

Some compilers will let you cast a pointer to be type size_t, however, that is not portable across all platforms and compilers and is generally not a good practice.

Presumably, the array tab is defined to be an array of Key structures:

So, to write portable code, write code something like this:

C++
int BinarySearch(int low, int high, struct Key x)
{
    int index = -1;
    int mid = 0;

    while (low <= high)
    {
        mid = (low + high) >> 1;

        if (firstKeyLessThanSecondKey(x, tab[mid]))
        {
            high = mid - 1;
        }
        else if (firstKeyLessThanSecondKey(tab[mid], x))
        {
            low = mid + 1;
        }
        else
        {
            index = mid;
        }
    }

    return index;
}


To search the entire tab array for a Key equal to x, pass 0 for low and the array length minus 1 for high.

This will return -1 if the Key that matches x is not found, otherwise the index into the array tab for the key that matches x will be returned.

Because the values of low and high are always positive, a shift can be used for the divide by 2.

C++
mid = (low + high) >> 1;


Of course, you'll have to modify this code for your application. I made some assumptions based on the book you cited.

Also, if the Key structure isn't very small, then instead of passing the Key structure by value, you might want to pass a pointer to a Key to both the BinarySearch function and to the FirstKeyLessThanSecondKey function.

Finally, I didn't compile and test this. I'm never 100% sure if code is correct until I debug it under varied conditions. If not correct, I expect this is very close to correct.

modified 2-Nov-13 0:51am.

QuestionListView::LVN_EndLabelEdit problem Pin
jackheroes31-Oct-13 8:51
jackheroes31-Oct-13 8:51 
SuggestionRe: ListView::LVN_EndLabelEdit problem Pin
Richard MacCutchan31-Oct-13 15:09
mveRichard MacCutchan31-Oct-13 15:09 
GeneralRe: ListView::LVN_EndLabelEdit problem Pin
jackheroes31-Oct-13 19:52
jackheroes31-Oct-13 19:52 
GeneralRe: ListView::LVN_EndLabelEdit problem Pin
Richard MacCutchan31-Oct-13 23:37
mveRichard MacCutchan31-Oct-13 23:37 
Questiontest Pin
Member 1036994131-Oct-13 5:37
Member 1036994131-Oct-13 5:37 
AnswerRe: test Pin
jeron131-Oct-13 6:01
jeron131-Oct-13 6:01 
Questionreturn value Pin
messages28-Oct-13 4:20
messages28-Oct-13 4:20 
AnswerRe: return value Pin
Joe79428-Oct-13 5:00
Joe79428-Oct-13 5:00 
GeneralRe: return value Pin
messages28-Oct-13 5:33
messages28-Oct-13 5:33 
AnswerRe: return value Pin
Aescleal28-Oct-13 6:11
Aescleal28-Oct-13 6:11 
GeneralRe: return value Pin
messages28-Oct-13 6:28
messages28-Oct-13 6:28 
QuestionSend POSITION through wParam Pin
_Flaviu27-Oct-13 22:04
_Flaviu27-Oct-13 22:04 
AnswerRe: Send POSITION through wParam Pin
Jochen Arndt27-Oct-13 22:23
professionalJochen Arndt27-Oct-13 22:23 
GeneralRe: Send POSITION through wParam Pin
_Flaviu27-Oct-13 22:35
_Flaviu27-Oct-13 22:35 
QuestionLAN port on/off LED Pin
coolerfantasy26-Oct-13 20:39
coolerfantasy26-Oct-13 20:39 
QuestionRe: LAN port on/off LED Pin
Richard MacCutchan26-Oct-13 21:37
mveRichard MacCutchan26-Oct-13 21:37 
AnswerRe: LAN port on/off LED Pin
coolerfantasy26-Oct-13 21:43
coolerfantasy26-Oct-13 21:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.