void pointer (void *) : how to determine type? 
Author Message
 void pointer (void *) : how to determine type?

hi, i am having trouble with a program which basically goes like this.

void main(void)
{
   int temp[10] = {1,2,3,4,5,6,7,8,9,0};
   char *temp2[4] = {"hi", "there", "how", "are"};

   my_sort(temp);
   my_sort(temp2);

Quote:
}

void my_sort(void *base)
{
   check_type(base);    /* the question is HOW to check what type is the array*/
   if (INTEGER) {
      /* then sort the integer array
        .
        .
        .
       */
   }
   else if (STRING) {
      /* then sort the array of strings
        .
        .
        .
       */
   else {
      /* other cases for arrays of other types
        .
        . etc
        . etc
       */

Quote:
}

The question is how to check the type of the array passed to my_sort.
I tried isdigit(base[0]) ,  but the compiler complains that I am dereferencing
a void pointer.  Similar functions from the ctype library also don't work.
It is a requirement of my program that the type be checked within my_sort, not
in main.

Any help appreciated!

Simon



Wed, 02 Apr 1997 10:47:13 GMT  
 void pointer (void *) : how to determine type?

Quote:

>hi, i am having trouble with a program which basically goes like this.

>void^H^H^H^Hint main(void)
>{
>   int temp[10] = {1,2,3,4,5,6,7,8,9,0};
>   char *temp2[4] = {"hi", "there", "how", "are"};

>   my_sort(temp);
>   my_sort(temp2);
return 0;
>}

>void my_sort(void *base)
>{
>   check_type(base); /* the question is HOW to check what type is the array*/
>   if (INTEGER) {
>      /* then sort the integer array
>       */
>   }
>   else if (STRING) {
>      /* then sort the array of strings
>       */
>   else {
>      /* other cases for arrays of other types
>       */
>}

>The question is how to check the type of the array passed to my_sort.

You can't.  Types are just a convention on how to interpret a set of
bits.  The only way to do this is to include the type as a parameter:

#define INT 0
#define STRING 1
...

/* or: enum types {INT, STRING, ...}; */

void my_sort(int type, void *base)
{
        switch(type) {
        case INT:
        {       int *ip=(int *)base;
                /* do stuff with with ip */
        }
        case STRING:
        {
                char **sp=(char **)base;
                /* do stuff with sp */
        }
        ...
        }

Quote:
}
>It is a requirement of my program that the type be checked within my_sort, not
>in main.

I realize your actual code may be different, but in the `main' function
as you have it here, you don't have to check the type, you KNOW it!

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Wed, 02 Apr 1997 15:06:29 GMT  
 void pointer (void *) : how to determine type?
[deletia]

Quote:
>>The question is how to check the type of the array passed to my_sort.
>You can't.  Types are just a convention on how to interpret a set of
>bits.  The only way to do this is to include the type as a parameter:
>#define INT 0
>#define STRING 1
>void my_sort(int type, void *base)
>{
>[deletia]  
>}
>>It is a requirement of my program that the type be checked within my_sort, not
>>in main.
>I realize your actual code may be different, but in the `main' function
>as you have it here, you don't have to check the type, you KNOW it!

Well I have to write a generic sorting functn which MUST have the prototype
 void qsort(void *base, size_t n, size_t sz, int (*comp)(void *a, void *b))
 where base is an array, n is the size of the array, sz is the size of an
 array element, and comp is some sort of comparing function.
 This is the same prototype as the library function qsort, although I will be
 using a different sorting algorithm.
I do NOT have control over what happens in main and what array types are passed
to function qsort, hence the need to determine the type of the array before I
can sort it.  Is this possible? How does the library function qsort (in stdlib.h) determine the type of the array ??


Wed, 02 Apr 1997 17:22:37 GMT  
 void pointer (void *) : how to determine type?

Quote:

>I do NOT have control over what happens in main and what array types are passed
>to function qsort, hence the need to determine the type of the array before I
>can sort it.  Is this possible? How does the library function qsort (in stdlib.h) determine the type of the array ??

It doesn't and it doesn't have to: To sort an array you have to be
able to compare two array elements, and swap them if neccessary.

For both tasks you don't have to know the type of the array elements.
It is enough to know their sizes (for swapping and addressing) and
how to compare them. (The comparison function will take pointers
to void as it's arguments).

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-2158




Wed, 02 Apr 1997 21:42:17 GMT  
 void pointer (void *) : how to determine type?

Quote:


>>I do NOT have control over what happens in main and what array types are
>>passed
>>to function qsort, hence the need to determine the type of the array before I
>>can sort it.  Is this possible? How does the library function qsort (in
>>stdlib.h) determine the type of the array ??

>It doesn't and it doesn't have to: To sort an array you have to be
>able to compare two array elements, and swap them if neccessary.

>For both tasks you don't have to know the type of the array elements.
>It is enough to know their sizes (for swapping and addressing) and
>how to compare them. (The comparison function will take pointers
>to void as it's arguments).

Knowing the size of an element and how to compare it with another
element is almost, but not quite, the same as knowing the element's
type.  There are more attributes a type can have (like how to print
it, etc.)  But the "type independence" of qsort is accomplished
by explicitly passing to it (1) the element size, and (2) the compare
function to use.  For sorting purposes, that's all you need to know
about a type.  
Of course you have to *know* what the type is when you *call*
qsort, and you have to write the different comparison functions
for all the different types!

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Thu, 03 Apr 1997 03:34:39 GMT  
 void pointer (void *) : how to determine type?

Quote:
>How does the library function qsort (in stdlib.h) determine the type of the array ??

It doesn't! It requires the user to provide the comparison function.
(User is programmer in this case).

Jari



Fri, 04 Apr 1997 00:07:27 GMT  
 void pointer (void *) : how to determine type?

Newsgroups: comp.lang.c
Subject: void pointer (void *) : how to determine type?
Organization: Helsinki University of Technology, Espoo, Finland.

Quote:
>How does the library function qsort (in stdlib.h) determine the type of the

array ??

It doesn't! It requires the user to provide the comparison function.
(User is programmer in this case).

Jari



Fri, 04 Apr 1997 01:07:27 GMT  
 void pointer (void *) : how to determine type?


    >> I realize your actual code may be different, but in the `main'
    >> function as you have it here, you don't have to check the type,
    >> you KNOW it!

    Simon> Well I have to write a generic sorting functn which MUST
    Simon> have the prototype

        void qsort(void *base, size_t n, size_t sz,
                int (*comp)(void *a, void *b))

    Simon> where base is an array,
    Simon> n is the size of the array, sz is the size of an array
    Simon> element, and comp is some sort of comparing function.  This
    Simon> is the same prototype as the library function qsort,
    Simon> although I will be using a different sorting algorithm.  I
    Simon> do NOT have control over what happens in main and what
    Simon> array types are passed to function qsort, hence the need to
    Simon> determine the type of the array before I can sort it.  Is
    Simon> this possible? How does the library function qsort (in
    Simon> stdlib.h) determine the type of the array ??

The way qsort is implememented, it does not require type information.
It steps through the individual elements of the array according to
how big they are.  Since the size returns the number of bytes, and
since a sizeof(char) == 1, you can assign base to a char *, and step
through that.  Comparisons are done by passing pointers to those
objects to the comp function.

There is no way to anticipate what the types of the elements of the
array are going to be.  The array could consist of structures of
arrays of pointers for all you know.  qsort doesn't care, it simply
assumes that the comp function will do the right thing for whatever
type that the elements of base might be.

-- James



Sun, 06 Apr 1997 02:41:51 GMT  
 void pointer (void *) : how to determine type?

Newsgroups: comp.lang.c
Subject: void pointer (void *) : how to determine type?
Organization: Washington University, St. Louis, MO


Quote:
CHIN) writes:


    >> I realize your actual code may be different, but in the `main'
    >> function as you have it here, you don't have to check the type,
    >> you KNOW it!

    Simon> Well I have to write a generic sorting functn which MUST
    Simon> have the prototype

        void qsort(void *base, size_t n, size_t sz,
                int (*comp)(void *a, void *b))

    Simon> where base is an array,
    Simon> n is the size of the array, sz is the size of an array
    Simon> element, and comp is some sort of comparing function.  This
    Simon> is the same prototype as the library function qsort,
    Simon> although I will be using a different sorting algorithm.  I
    Simon> do NOT have control over what happens in main and what
    Simon> array types are passed to function qsort, hence the need to
    Simon> determine the type of the array before I can sort it.  Is
    Simon> this possible? How does the library function qsort (in
    Simon> stdlib.h) determine the type of the array ??

The way qsort is implememented, it does not require type information.
It steps through the individual elements of the array according to
how big they are.  Since the size returns the number of bytes, and
since a sizeof(char) == 1, you can assign base to a char *, and step
through that.  Comparisons are done by passing pointers to those
objects to the comp function.

There is no way to anticipate what the types of the elements of the
array are going to be.  The array could consist of structures of
arrays of pointers for all you know.  qsort doesn't care, it simply
assumes that the comp function will do the right thing for whatever
type that the elements of base might be.

-- James



Sun, 06 Apr 1997 03:41:51 GMT  
 void pointer (void *) : how to determine type?

Quote:

> hi, i am having trouble with a program which basically goes like this.
> void main(void)
> {
>    int temp[10] = {1,2,3,4,5,6,7,8,9,0};
>    char *temp2[4] = {"hi", "there", "how", "are"};
>    my_sort(temp);
>    my_sort(temp2);
> }
> void my_sort(void *base)
> {
>    check_type(base);    /* the question is HOW to check what type is the array*/
>    if (INTEGER) {
>       /* then sort the integer array
>    .
>    .
>    .
>        */
>    }
>    else if (STRING) {
>       /* then sort the array of strings
>    .
>    .
>    .
>        */
>    else {
>       /* other cases for arrays of other types
>    .
>    . etc
>    . etc
>        */
> }
> The question is how to check the type of the array passed to my_sort.
> I tried isdigit(base[0]) ,  but the compiler complains that I am dereferencing
> a void pointer.  Similar functions from the ctype library also don't work.
> It is a requirement of my program that the type be checked within my_sort, not
> in main.

You don't check it.  You change the design so that the information is
passed to the function in some way.

--
Mike Rubenstein



Thu, 10 Apr 1997 04:40:09 GMT  
 void pointer (void *) : how to determine type?
From: rubenst%occs.nlm.nih.gov (Michael M. Rubenstein)
Newsgroups: comp.lang.c
Subject: void pointer (void *)  : how to determine type?
Organization: National Library of Medicine

Quote:

> hi, i am having trouble with a program which basically goes like this.
> void main(void)
> {
>    int temp[10] = {1,2,3,4,5,6,7,8,9,0};
>    char *temp2[4] = {"hi", "there", "how", "are"};
>    my_sort(temp);
>    my_sort(temp2);
> }
> void my_sort(void *base)
> {
>    check_type(base);    /* the question is HOW to check what type is the
array*/
>    if (INTEGER) {
>       /* then sort the integer array
>    .
>    .
>    .
>        */
>    }
>    else if (STRING) {
>       /* then sort the array of strings
>    .
>    .
>    .
>        */
>    else {
>       /* other cases for arrays of other types
>    .
>    . etc
>    . etc
>        */
> }
> The question is how to check the type of the array passed to my_sort.
> I tried isdigit(base[0]) ,  but the compiler complains that I am
dereferencing
> a void pointer.  Similar functions from the ctype library also don't work.
> It is a requirement of my program that the type be checked within my_sort,
not
> in main.

You don't check it.  You change the design so that the information is
passed to the function in some way.

--
Mike Rubenstein



Thu, 10 Apr 1997 05:40:09 GMT  
 
 [ 11 post ] 

 Relevant Pages 

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

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

3. difference between void foo(void) and void foo()

4. Determining type of void *

5. Determining type of void *

6. void type pointers

7. passing pointers of type void?

8. casting a structure type on a void pointer in the Watch window

9. Type-cast from a void pointer

10. HRESULT return type turns into void when adding class from type library

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

12. difference between void and <bold>void

 

 
Powered by phpBB® Forum Software