简体   繁体   中英

pointer arithmetic and “generic” in c

I tried to put program a generic method in C to identify the biggest element of an array.

At first, I programmed this:

int compare(const void* a, const void* b) {
    if(a < b)
        return 0;

    return 1;
}

int main(void) {

    int (*prt)(const void*, const void*);
    prt=compare;

    printf("%i",(*prt)(1,1));

    return EXIT_SUCCESS; 
}

This works fine, but if I try to put the function pointer prt in a new method, I do not know how to handle it. Addionally i dont know how to handle void* types.

void* maximum(int len, void* array, size_t size, int (*cmp)(const void*, const void*));

int compare(const void* a, const void* b) {
    if(a < b)
        return 0;

    return 1;
}

int main(void) {

    int (*prt)(const void*, const void*);
    prt=compare;

    printf("%i",(*prt)(1,1));

    int array[6] = {3, 1, 0 , 4 , 3, 9};

    maximum(len,array,0,prt);

    return EXIT_SUCCESS;
}

void* maximum(int len, void* array, size_t size, int (*cmp)(const void*, const void*)) {
    void* temp;

    temp = array[0];

    printf("%i",a);
    int i;
    for(i = 1; i < len; i++) {
        if((*cmp)(temp,array[i]) == 0) {
            temp = array[i];
        }
    }

    return 0;
}

There are many errors... eg the variable temp or if((*cmp)(temp,array[i]) == 0) .

Do you have an idea how to use not defined datatypes?

You are comparing addresses instead of values:

int compare(const void* a, const void* b) {
    if(a < b)
        return 0;

    return 1;
}

Should be:

int compare(const void* a, const void* b) {
    if(*(int *)a < *(int *)b)
        return 0;

    return 1;
}
#include <stdio.h>
#include <stdlib.h>

void *maximum(int len, void* array, size_t size, int (*cmp)(const void*, const void*));

//comparison function must know about type. 
//Because it is not known for functions like memcmp that type  what is the layout.
int intcmp(const int *x, const int *y){
    return *x < *y ? -1 : *x > *y;
}

int main(void) {
    int array[6] = {3, 1, 0 , 4 , 3, 9};

    int *p = maximum(sizeof(array)/sizeof(*array), array, sizeof(*array), (int (*)(const void*,const void*))intcmp);
    printf("%d\n", *p);//9

    return EXIT_SUCCESS;
}

void *maximum(int len, void *array, size_t size, int (*cmp)(const void*, const void*)) {
    int i;
    void *temp = array;

    for(i = 1; i < len; i++) {
        if(cmp((char*)array + size*i, temp)>0) {
            temp =  (char*)array + size*i;
        }
    }

    return temp;
}

Here is an example that can be taken as a base code.

#include <stdio.h>

int cmp( const void *a, const void *b )
{
    return *( const int * )a < *( const int * )b;
}

void * maximum( const void *array, size_t size, size_t len, 
                int cmp( const void *, const void *) ) 
{
    const void *max = array;
    size_t i = 1;

    for ( ; i < size; i++ ) 
    {
        if ( cmp( ( const char * )max, ( const char * )array + i * len ) )
        {
            max = ( const char * )array + i * len;
        }
    }

    return ( void * )max;
}

int main(void) 
{
    int array[] = { 3, 1, 0 , 4 , 3, 9 };

    int *max = 
        maximum( array, sizeof( array )/ sizeof( *array ), sizeof( int ), cmp );

    printf( "Maximum = %d\n", *max );

    return 0;
}

The output is

Maximum = 9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM