typedef int COMPARE(const void *, const void *); 
Author Message
 typedef int COMPARE(const void *, const void *);

What is the meaning of typedef used below :

typedef int COMPARE(const void *, const void *);

Does it a TYPE of a function with 2 pointers inputs
and return an integer ?

Does it declare as below ?
  COMPARE test; /* declare test */

After declared, how can I use it ?  Does it like below ?
  char *a;
  char *b;
  test(a, b);

Do I need to implement this funcion COMPARE after type defined ?

When I define a STRING type in c, I will do it as below :
  typedef char STRING[128];

and use it by :
  STRING test;  /* then test is a string with 128 bytes memory spaces
allocated */

What is the difference of typedef between "COMPARE" and "STRING" ???

Thanks all !



Sun, 07 Oct 2001 03:00:00 GMT  
 typedef int COMPARE(const void *, const void *);

Quote:

> What is the meaning of typedef used below :

> typedef int COMPARE(const void *, const void *);

It defines COMPARE to be the name of a type for functions taking two const void
* arguments and returning an int.  Just what it says.

Quote:

> Does it a TYPE of a function with 2 pointers inputs
> and return an integer ?

That is not a coherent English question about C.

Quote:

> Does it declare as below ?
>   COMPARE test; /* declare test */

'It' does no such thing.  You could use COMPARE that way.

Quote:

> After declared, how can I use it ?  Does it like below ?
>   char *a;
>   char *b;
>   test(a, b);

> Do I need to implement this funcion COMPARE after type defined ?

Of course it needs to be implemented.  Did you think that Dennis Ritchie would
descend from BellLabs and do it for you?

Quote:

> When I define a STRING type in c, I will do it as below :
>   typedef char STRING[128];

> and use it by :
>   STRING test;  /* then test is a string with 128 bytes memory spaces
> allocated */

You could do that, if you really needed Pascal.

Quote:

> What is the difference of typedef between "COMPARE" and "STRING" ???

They define different types with different identifiers.  There really is no
mystery here.

Quote:
> Thanks all !

--




Sun, 07 Oct 2001 03:00:00 GMT  
 typedef int COMPARE(const void *, const void *);
: What is the meaning of typedef used below :

: typedef int COMPARE(const void *, const void *);

: Does it a TYPE of a function with 2 pointers inputs
: and return an integer ?

Yes.

: Does it declare as below ?
:   COMPARE test; /* declare test */

Yes.  But I don't think that's a good idea.  It's pretty
confusing.

: After declared, how can I use it ?  Does it like below ?
:   char *a;
:   char *b;
:   test(a, b);

: Do I need to implement this funcion COMPARE after type defined ?

Well, yes.  All you have is a type, like an int - you still have
to declare a specific variable.

: When I define a STRING type in c, I will do it as below :
:   typedef char STRING[128];

: and use it by :
:   STRING test;  /* then test is a string with 128 bytes memory spaces
: allocated */

Don't do that.  Really, don't.  You'll get yourself and everybody else
very confused.

: What is the difference of typedef between "COMPARE" and "STRING" ???

I don't really understand the question; they are different types of
object, since that's how you've written the typedefs.

Will



Sun, 07 Oct 2001 03:00:00 GMT  
 typedef int COMPARE(const void *, const void *);

Quote:

> : typedef int COMPARE(const void *, const void *);
> : Does it a TYPE of a function with 2 pointers inputs
> : and return an integer ?
> Yes.

Sorry Sir, I still make confusing about this.  What are the input pointers
for ?
Particular the pointer pointing to void type !!...  Does it mean for an
address passing
into this type "COMPARE" ?  Will we use the contents resided in that
address ?

Quote:

> : Do I need to implement this funcion COMPARE after type defined ?

> Well, yes.  All you have is a type, like an int - you still have
> to declare a specific variable.

um.....  if the "COMPARE" is for a comparison operation, would anyone show
me an outline of this "typedef" implementation ?


Tue, 09 Oct 2001 03:00:00 GMT  
 typedef int COMPARE(const void *, const void *);

Quote:

> > : typedef int COMPARE(const void *, const void *);
> > : Does it a TYPE of a function with 2 pointers inputs
> > : and return an integer ?
> > Yes.

> Sorry Sir, I still make confusing about this.  What are the input
pointers
> for ?
> Particular the pointer pointing to void type !!...  Does it mean for an
> address passing
> into this type "COMPARE" ?  Will we use the contents resided in that
> address ?

> > : Do I need to implement this funcion COMPARE after type defined ?

> > Well, yes.  All you have is a type, like an int - you still have
> > to declare a specific variable.

> um.....  if the "COMPARE" is for a comparison operation, would anyone
show
> me an outline of this "typedef" implementation ?

As it's Christmas (on whatever planet) I'll oblige...

Let's say we want to do a binary search on an array of structs. I've
submitted this lot to a quick test-compile, to avoid too much risk of a red
face later on.

#include <stdio.h>
#include <stdlib.h> /* for bsearch - _pace_ hunsoo */

#define MAX_BUFF 32

typedef struct FOO
{
  int Bar;
  char Baz[MAX_BUFF + 1];

Quote:
} FOO;

/* This typedef and the following declaration are
 * not my recommended way of doing things - merely
 * pointing out to the OP how the construct works.
 */

/* Define a new type: objects of type COMPARE are
 * functions taking two const void *, and returning int.
 */
typedef int COMPARE(const void *, const void *);

/* Therefore, Compare is a function taking two const void *
 * and returning int. The function is NOT defined here.
 * It will be defined later on, don't worry.
 */
COMPARE Compare;

/* Remember, main returns int. (But you knew that already, right?)*/

int main(void)
{

  /* Here is the array of structs we wish to search. Note that
   * they are in ascending order of their 'Bar' member. If they
   * weren't, we could call qsort to sort them. Here's the call
   * you would need: qsort(foo, elements, sizeof foo[0], Compare);
   */
  FOO foo[] =
  {
    { 12, "Hello" }, { 37, "there" },
    { 54, "I"     }, { 59, "hope"  },
    { 63, "all"   }, { 68, "will"  },
    { 70, "soon"  }, { 77, "be"    },
    { 82, "clear" }, { 85, "thanks"},
    { 86, "to"    }, { 88, "the"   },
    { 90, "magic" }, { 93, "of"    },
    { 99, "Usenet"}
  };

  /* How many elements in the array? */
  int elements = sizeof foo / sizeof foo[0];

  /* Declare a key and a pointer to catch the search result */
  FOO key = {0};
  FOO *Result = NULL;

  /* Load the key - forgive me for obfuscating it... there is of course no
need. */

  key.Bar = (((1 << 4) | 37) << 1) - (1 << 4);

  /* Search the array for a struct matching the key. Note that the
   * address of the function 'Compare' is passed to bsearch, which
   * will call it a number of times.
   */
  Result = bsearch(&key, foo, elements, sizeof foo[0], Compare);

  /* bsearch returns NULL if the struct can't be found */  
  if(NULL != Result)
  {
    printf("Result of search: Success! The answer is %s!\n", Result->Baz);
  }
  else
  {
    printf("Search failed to find a match for %d\n", key.Bar);
  }

  /* main returns int. I think I mentioned that already. */
  return 0;

Quote:
}

/* Here is the comparison function in all its glory. */
int Compare(const void *p1, const void *p2)
{
  const FOO *f1 = p1;
  const FOO *f2 = p2;

  /* Put a breakpoint on the return statement,
   * and then step through your code in the
   * de{*filter*}, to see what happens.
   */

  return f1->Bar - f2->Bar;

Quote:
}

--
Richard Heathfield

The bug stops here.



Tue, 09 Oct 2001 03:00:00 GMT  
 typedef int COMPARE(const void *, const void *);

:> : typedef int COMPARE(const void *, const void *);
:> : Does it a TYPE of a function with 2 pointers inputs
:> : and return an integer ?
:> Yes.
:>

: Sorry Sir, I still make confusing about this.  What are the input pointers
: for ?
: Particular the pointer pointing to void type !!...  Does it mean for an
: address passing
: into this type "COMPARE" ?  Will we use the contents resided in that
: address ?

:>
:> : Do I need to implement this funcion COMPARE after type defined ?
:>
:> Well, yes.  All you have is a type, like an int - you still have
:> to declare a specific variable.

: um.....  if the "COMPARE" is for a comparison operation, would anyone show
: me an outline of this "typedef" implementation ?

Look up the standard library qsort() function, and you'll see an example
of how this sort of function is declared and used.

Will



Tue, 09 Oct 2001 03:00:00 GMT  
 typedef int COMPARE(const void *, const void *);

Quote:

> :>
> :> : Do I need to implement this funcion COMPARE after type defined ?
> :>
> :> Well, yes.  All you have is a type, like an int - you still have
> :> to declare a specific variable.

> : um.....  if the "COMPARE" is for a comparison operation, would anyone show
> : me an outline of this "typedef" implementation ?

> Look up the standard library qsort() function, and you'll see an example
> of how this sort of function is declared and used.

But in the stdlib.h,  I only find 2 relevant lines about qsort ( ) :
  extern void qsort(void *, size_t, size_t, int (*)(const void *, const void
*));
  extern void qsort();

I think they are declaration only and can be called by others everywhere.  Am I
right ?
Is there any other part that contains the implementation of "qsort( ) " ??



Wed, 10 Oct 2001 03:00:00 GMT  
 typedef int COMPARE(const void *, const void *);

:> :>
:> :> : Do I need to implement this funcion COMPARE after type defined ?
:> :>
:> :> Well, yes.  All you have is a type, like an int - you still have
:> :> to declare a specific variable.
:>
:> : um.....  if the "COMPARE" is for a comparison operation, would anyone show
:> : me an outline of this "typedef" implementation ?
:>
:> Look up the standard library qsort() function, and you'll see an example
:> of how this sort of function is declared and used.

: But in the stdlib.h,  I only find 2 relevant lines about qsort ( ) :
:   extern void qsort(void *, size_t, size_t, int (*)(const void *, const void
: *));
:   extern void qsort();

: I think they are declaration only and can be called by others everywhere.  Am I
: right ?
: Is there any other part that contains the implementation of "qsort( ) " ??

Yes, qsort() is part of the standard library.  Most textbooks, and the
FAQ, discuss its use.  The FAQ has an example of the compare() function
for strings.

Will



Wed, 10 Oct 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. typedef void PF(int, void *);

2. void foo(const int a[2][3])

3. Type of void foo(const int) ?

4. what's the difference between void main(void) and int main(void)

5. from void (void*) to void (_cdecl*) (void*)

6. const *void also can be a generic pointer?

7. function pointer returning const * incompatible with void *?

8. void *buf in ssize_t read(int fildes, void *buf, size_t nbyte)

9. Passing literal as const void*?

10. writing a (const void*) to a file.

11. void main(void) or int main() ??

12. Help needed with const void * parameters

 

 
Powered by phpBB® Forum Software