Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

void* considered harmful

Name: Anonymous 2012-12-22 14:24


#include "stdio.h"

void *lsearch(void *addr, void *key, int arraysize, int typesize) {
        for (int i = 0; i < arraysize; i++) {
                if (addr[typesize * i] == *key) return &addr[typesize * i];
        }
        return NULL;
}
 
int main() {
        int is[] = {1,2,3,4,5};
        int i = 5;
        void *address = lsearch(is, &i, 5, sizeof(int));
        printf("%p", address);
        return 0;
}



why do I get these errors?:


generics.c: In function ‘lsearch’:
generics.c:5:11: warning: dereferencing ‘void *’ pointer [enabled by default]
generics.c:5:29: warning: dereferencing ‘void *’ pointer [enabled by default]
generics.c:5:3: error: void value not ignored as it ought to be
generics.c:5:3: error: void value not ignored as it ought to be
generics.c:5:47: warning: dereferencing ‘void *’ pointer [enabled by default]

Name: Anonymous 2012-12-22 18:23

>>6
This code gives no warnings on my end
Pointer arithmetic on void pointers yields undefined behavior, so I wouldn't count on your code for a lot.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List