Re: void *



Roman Mashak wrote, On 28/06/07 21:35:
"Barry Schwarz" <schwarzb@xxxxxxxxx> wrote in message news:2cd683lech1rbc5v2vvd1jfvth7j9tug70@xxxxxxxxxx
struct list_node
{
void *data;
struct list_node *next;
};
It would probably help if your structure had a member which identified
the real type of whatever data pointed to.

But the structure can't have members for all possible types. And I expected 'void *' is to hold _pointers_ on the objects of various types (perhaps I wasn't very clear in my original post), i.e. I could you something like this:

int main(void)
{
node *p = NULL;

list_add_head(&p, (int *)100);

This is *not* getting a pointer to an int, it is coercing an int in to an int pointer. There is no guarantee that all int values can be converted to pointers.

list_add_head(&p, (char *)'X');

This is converting another int (not char), this time to a pointer to char. Same problem.

...
return 0;
}

That was my understanding of 'void *' concept.

The concept of void* is what you described in your text, *not* what you show in your code. I.e.

void *p;
int i;
char c;
p = &i;
p = &c;

Note that it does *not* require a cast.

In fact at your level the first thing you should assume when you appear to need a cast is that you have done something wrong and a cast is *not* the solution. If it appears that you have not done anything wrong then your next assumption should be that you have completely misunderstood something fundamental and done something very wrong.

There are a few places where casts are required, but not in general where you would expect.

typedef struct list_node node;

node* list_add_head(node **head, void *data)
{
node *n= malloc(sizeof *n);
/* check if n==NULL ...*/

n->data = (void *)data; /* is that correct use? */
n->data is a void*. data is a void*. What do you think the cast
accomplishes?


So, n->data = data is enough?

When two variables are of the same type of *course* it is.

Personally I don't think you understand the concept of pointers yet, so you need to read the sections of your text book and the comp.lang.c FAQ that refer to pointers. The comp.lang.c FAQ is available at http://c-faq.com/
--
Flash Gordon
.



Relevant Pages

  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • Re: int a[5] .. difference between a and &a
    ... int main{ ... Invoking undefined behaviour can give any result whatsoever. ... And what lesson will the OP draw from your apparent condoning of the cast ... void main can set your house on fire. ...
    (comp.lang.c)
  • Re: Using a link list over an array.
    ... compile (p->data is a void *) so you have not shown us some key ... You can't use it to sort a list of ints where the int is ... You can use it to sort a list of pointers to any type. ...
    (comp.lang.c)
  • [RFC] timers, pointers to functions and type safety
    ... * they have callback of type void ... callback is called by the code that even in theory has no ... The last one means that we can pass any pointers to these suckers; ... cast to unsigned long and cast back in the callback. ...
    (Linux-Kernel)
  • Re: [9fans] porting help please (gcc void pointer handling)
    ... "Pointer arithmetic cannot be performed on void pointers because the ... the cast to an unsigned char * cast may not necessarily be the solution ... Comeau C/C++ with Dinkumware's Libraries... ...
    (comp.os.plan9)