简体   繁体   中英

Deferencing Void Pointer in c

i'm having troubles trying to deference a void pointer in C.

Well, i make a linked list that have a "Nodo" with a void* info, that's because info will change of type.

typedef struct nodo
{
    void *info; //This'll change types, like different structs.
    char label; // S si es un switch, E si es un enemigo
    struct nodo *siguiente;
}tNodo;

typedef struct Lista {
    tNodo *head;
    tNodo *tail;
    tNodo *curr;
    unsigned int listSize;
    unsigned int pos;
}tLista;

Here's my struct tEnemigo, this'll be one of the struct that info could take.

typedef struct
{
    char(*siguiente_movimiento)(void *,char **,int,int);
    char tipo;
    int maxmov;
    int pasos;
    char direccion;
    int y;
    int x;
}tEnemigo;

In this function i just pass void *general to take the info of it

char movEnemigos(tLista *listaEnemigos, char **map)
{
void *general;
general = &listaEnemigos->curr->info; //A struct 
siguiente_movimiento(general, map , x, y);
}

And here's my problem, i can't give the "info" that is located general, i read something about cast the void pointer, but i failed miserably.

char siguiente_movimiento(void *general, char **map, int x, int y)
{
tEnemigo *enemigo;
enemigo = *(tEnemigo *) general ;
}

I don't know how to do this, maybe i'm wrong with all my "idea" of the code...hope you guys can give me a hand.

EDIT: THIS IS THE ERROR CODE.

error: incompatible types when assigning to type ‘tEnemigo’ from type ‘struct tEnemigo      *’
      *(tEnemigo *) general = enemigo;

In your siguiente_movimiento function, enemigo is a pointer, but you are trying to assign a value to it that is not a pointer (because you dereference it with * ). Your function should look like this:

char siguiente_movimiento(void *general, char **map, int x, int y)
{
    tEnemigo enemigo;
    enemigo = *(tEnemigo *) general ;
}

Or:

char siguiente_movimiento(void *general, char **map, int x, int y)
{
    tEnemigo *enemigo;
    enemigo = (tEnemigo *) general ;
}

You currently have a mix of the two.

The error message gives the game away — you haven't declared a type struct tEnemigo .

You have:

typedef struct
{
    ...
}tEnemigo;

You either need to drop the struct when using the type, or you need:

typedef struct tEnemigo
{
    ...
} tEnemigo;

Both techniques work. I'd probably add the tag anyway, but I'd definitely write the code in terms of the typedef name (just tEnemigo ), not struct tEnemigo .

And, as a side note, you should aim to avoid void * arguments. One advantage of the struct tEnemigo tag is that you can define opaque types by simply saying:

struct tEnemigo;   // There is a type struct tEnemigo...

char siguiente_movimiento(struct tEnemigo *enemigo, char **map, int x, int y);

Your code that uses struct tEnemigo pointers does not need to know about the internals of the structure; only code that needs to look inside the structure needs to know the full type definition.

One reason for avoiding void * arguments is that any pointer type can be passed to a function that expects a void * . For example, if you have a FILE *fp = fopen("something", "r); , then the compiler cannot diagnose an error in:

char c = siguiente_movimiento(fp, map, 1, 3);

even though it is clearly a mistake. With the struct tEnemigo * declaration for the function, this mistake becomes a compilation error.

Rule of Thumb: if the void * argument only maps to a single type, then the chances are good that the function shouldn't be written with a void * argument. (There are exceptions: for example, if you have a family of functions called by function pointer, you might need a void * argument so that a single call can call any of the functions. Memory allocation functions are another special case.)

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