Arithmetic operation in pointer using C
-
Hi friends, I tried out something like this which is given below in VC++ by referring Dennis M. Ritchie (ANSI C second edition) book examples. struct Key *low = &tab[0]; struct Key *high = &tab[n]; struct Key *mid; mid = low + (high-low) / 2; // Error: cannot convert from 'int' to 'struct Key *' My question is how they are using this expression mid = low + (high-low) / 2;?. I don't know whether I misunderstood the concept or there is some difference between ANSI C and VC++. Please anyone help me in this. Regards, S.Shanmuga Raja
-
Hi friends, I tried out something like this which is given below in VC++ by referring Dennis M. Ritchie (ANSI C second edition) book examples. struct Key *low = &tab[0]; struct Key *high = &tab[n]; struct Key *mid; mid = low + (high-low) / 2; // Error: cannot convert from 'int' to 'struct Key *' My question is how they are using this expression mid = low + (high-low) / 2;?. I don't know whether I misunderstood the concept or there is some difference between ANSI C and VC++. Please anyone help me in this. Regards, S.Shanmuga Raja
-
Hi friends, I tried out something like this which is given below in VC++ by referring Dennis M. Ritchie (ANSI C second edition) book examples. struct Key *low = &tab[0]; struct Key *high = &tab[n]; struct Key *mid; mid = low + (high-low) / 2; // Error: cannot convert from 'int' to 'struct Key *' My question is how they are using this expression mid = low + (high-low) / 2;?. I don't know whether I misunderstood the concept or there is some difference between ANSI C and VC++. Please anyone help me in this. Regards, S.Shanmuga Raja
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 arraytab
is defined to be an array ofKey
structures: So, to write portable code, write code something like this: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 tox
, pass 0 forlow
and the array length minus 1 forhigh
. This will return -1 if theKey
that matchesx
is not found, otherwise the index into the arraytab
for the key that matchesx
will be returned. Because the values oflow
andhigh
are always positive, a shift can be used for the divide by 2.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 theKey
structure by value, you might want to pass a pointer to a Key to both theBinarySearch
function and to theFirstKeyLessThanSecondKey
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.