V
V.Subramanian, India
I am using Intel Pentium D processor-based RedHat operating system
with gcc version 3.4.3
For a file named x.c, I compile it with the following
command:
gcc -std=c99 -pedantic -Wall -Wextra x.c
Consider the following functions:
inline int cmpfnForAscendingOrder(
KeyType firstKey,
KeyType secondKey)
{
int ret;
if (firstKey < secondKey)
ret = -1;
else if (firstKey == secondKey)
ret = 0;
else
ret = 1;
return ret;
}
inline int cmpfnForDescendingOrder(
KeyType firstKey,
KeyType secondKey)
{
return -cmpfnForAscendingOrder(
firstKey,
secondKey);
}
bool deleteLastKNodesWithKeyInSortedList(
NoOfNodes k,
KeyType key,
int (*compareKey)(KeyType firstKey,
KeyType secondKey),
ListNode **node)
{
// ...
int (*tempCompareKey)(
KeyType firstKey,
KeyType secondKey);
// Is the following allowed ?
// Note that it works in my implementation.
tempCompareKey =
(compareKey ==
&cmpfnForAscendingOrder)
? &cmpfnForDescendingOrder
: &cmpfnForAscendingOrder;
// ...
}
I invoke the function deleteLastKNodesWithKeyInSortedList() with
appropriate arguments. One of them is a function pointer which is the
third parameter. For this third parameter, I pass either
'cmpfnForAscendingOrder' or
'cmpfnForDescendingOrder'.
Now inside deleteLastKNodesWithKeyInSortedList(),
I need to know whether the third parameter 'compareKey' holds
'cmpfnForAscendingOrder' or
'cmpfnForDescendingOrder' and assign one of these two function
pointers(in fact revert) to the variable 'tempCompareKey'. For this, I
do an equality comparison check as follows:
(compareKey == &cmpfnForAscendingOrder)
Is this equality comparison check of function pointers, legal, correct
and allowed ? (Note that it works in my implementation of gcc. But I
do not want to go by one particular implementation).
Please explain.
Thanks
V.Subramanian
India
with gcc version 3.4.3
For a file named x.c, I compile it with the following
command:
gcc -std=c99 -pedantic -Wall -Wextra x.c
Consider the following functions:
inline int cmpfnForAscendingOrder(
KeyType firstKey,
KeyType secondKey)
{
int ret;
if (firstKey < secondKey)
ret = -1;
else if (firstKey == secondKey)
ret = 0;
else
ret = 1;
return ret;
}
inline int cmpfnForDescendingOrder(
KeyType firstKey,
KeyType secondKey)
{
return -cmpfnForAscendingOrder(
firstKey,
secondKey);
}
bool deleteLastKNodesWithKeyInSortedList(
NoOfNodes k,
KeyType key,
int (*compareKey)(KeyType firstKey,
KeyType secondKey),
ListNode **node)
{
// ...
int (*tempCompareKey)(
KeyType firstKey,
KeyType secondKey);
// Is the following allowed ?
// Note that it works in my implementation.
tempCompareKey =
(compareKey ==
&cmpfnForAscendingOrder)
? &cmpfnForDescendingOrder
: &cmpfnForAscendingOrder;
// ...
}
I invoke the function deleteLastKNodesWithKeyInSortedList() with
appropriate arguments. One of them is a function pointer which is the
third parameter. For this third parameter, I pass either
'cmpfnForAscendingOrder' or
'cmpfnForDescendingOrder'.
Now inside deleteLastKNodesWithKeyInSortedList(),
I need to know whether the third parameter 'compareKey' holds
'cmpfnForAscendingOrder' or
'cmpfnForDescendingOrder' and assign one of these two function
pointers(in fact revert) to the variable 'tempCompareKey'. For this, I
do an equality comparison check as follows:
(compareKey == &cmpfnForAscendingOrder)
Is this equality comparison check of function pointers, legal, correct
and allowed ? (Note that it works in my implementation of gcc. But I
do not want to go by one particular implementation).
Please explain.
Thanks
V.Subramanian
India