Need help on pointer to VOID

This is a discussion on Need help on pointer to VOID within the C Programming forums, part of the General Programming Boards category; Can anyone explain concisely about pointer variable which points to VOID such as:
void * void_prt;
How does memory space ...

The only difference when it comes to void* pointers is that you can't dereference a void pointer (and you can't do sizeof(void) either).

For a char *ptr, this is OK
char c, *p = &c;
*p = 'a';

The only difference is that you can't do
*p = 'a';
with a void pointer

You might say
void *p = malloc( 20 );
You know p points to 20 bytes, but there is nothing you can say about the content of those 20 bytes (they're undefined anyway), since you can never do a *p type operation to look at what the pointer points to.

A NULL pointer is a pointer to nothing (as opposed to a pointer to 20 bytes which you know nothing about)

You can have a NULL pointer of any pointer type
char *p = NULL;
void *q = NULL;

You can't dereference NULL pointers of any type either
*p = 'a';
If p was char*, then it would likely fail at run-time with some kind of protection exception.
If p was void*, the compiler would complain about trying to dereference a void pointer.

Thanx for your help, Salem.
But base on what you are explaining, how can you
know the adress of a VOID pointer variable?
And since you can't dereference it, then it seem
like a unuseful data type for creating data structures

I have seen pointers to VOID usually use in
real-time embedded programs. Do you know why?
What the main purpose of using Pointers To VOID?
and when (in what case) do we need to use it?
Again, Gracias!
DV007

You actually can dereference a void pointer. You just have to type cast it to whatever type you're dereferencing it as. If it points to an int, dereference it as if you were using a pointer to an integer by type casting.

Thanxs a lot, guys. Y'all are really helpfull.
It seems like Pointer to VOID has a good advantage
in data manipulation??? because you can type cast to it with different data type(int, float, char, struct..), but since C is not STRONG type-checking
and no GARBAGE collection, I think it's dangerous
to use or it needs very careful to use in those
programs involve with human-live risk such as
Fligh Control application. Do you guys agree?
It's nice to discuss with y'all.

abt VOID *

hey guys

Quzah seems to be a the master of C......

i feel VOID pointers can be used where u have to decide the type of pointer u need at the runtime ,in such cases u declare tht as a pointer to void and type cast it at runtime ,to whtever type u need, int ,char , float ,anything.

Re: abt VOID *

Originally posted by ucsbme hey guys

Quzah seems to be a the master of C......

i feel VOID pointers can be used where u have to decide the type of pointer u need at the runtime ,in such cases u declare tht as a pointer to void and type cast it at runtime ,to whtever type u need, int ,char , float ,anything.

and sizeof (void*) doesnt make sense, it is absurd

also void* a and a++ is also absurd

every object has a size , sizeof(void *) makes perfect sense ,
i.e.,
if I declare
void *p;
p is an object , it takes up some size in memory , sizeof (p) or sizeof(void *) will tell us how many bytes it takes up
sizeof(void) doesnt make sense though , as there is no type named void , if sizeof(void *) didnt make sense we would not be able to dynamically allocate a particular number of void size pointers .
say , I need 20 void * pointers
so, I do

Re: Re: abt VOID *

Originally posted by Unregistered
every object has a size , sizeof(void *) makes perfect sense ,
i.e.,
if I declare
void *p;
p is an object , it takes up some size in memory , sizeof (p) or sizeof(void *) will tell us how many bytes it takes up
sizeof(void) doesnt make sense though , as there is no type named void , if sizeof(void *) didnt make sense we would not be able to dynamically allocate a particular number of void size pointers .

Hm. Close. 'sizeof( void* )' only give you the size of the pointer. It doesn't give you the size of what it points at, because what it poitns at could be anything.

All pointers take up exactly the same amount of space, IIRC. It is simply a memory address. This is basicly why void poitners work.

Prelude states that sizeof doesn't work correctly with dynamicly allocated memory. I suspect this goes doubly for trying to get the size of something a void pointer points at.

That said, no, you cannot simply:

int x;
void *v = &x;

sizeof( v );

...and expect it to tell you the size of the integer (object) it points at. In this case, the size would like ly happen to be the same, because the size of an ingeter on a 32 bit compiler happens to be the same size as a pointer.

>I suspect this goes doubly for trying to get the size of something a void pointer points at.
It does, in a way. If you try to get the size of a void pointer then you will simply get the size of a pointer, but if you cast and dereference a void pointer then sizeof will return the size of the data type you cast the void pointer to. Pretty straight forward in it's own twisted way, but what's really interesting is when you dereference a void pointer to a char pointer you get the size of a char and not a char pointer. Here's a fun little test to see just what can happen:

Re: Re: Re: abt VOID *

Why do you misrepresent what I wrote ?
sizeof (a) will tell you what the size of a is and not what it points to (I did not imply that either directly or indirectly ) a might not even be a pointer ,similarly sizeof(void *) tells you what the sizeof of a "void *" is (which happens to be a pointer type), and since there are objects of type "void *" , it will tell you how much space one object of type "void *" will take ,just as sizeof(int *) will tell what the size of an object of type "int *" is ,not the size of int . sizeof works perfectly as it is supposed to , nothing breaks down in case of dynamically allocated memory .

Originally posted by quzah

Hm. Close. 'sizeof( void* )' only give you the size of the pointer. It doesn't give you the size of what it points at, because what it poitns at could be anything.

Originally posted by Prelude >I suspect this goes doubly for trying to get the size of something a void pointer points at.
It does, in a way. If you try to get the size of a void pointer then you will simply get the size of a pointer, but if you cast and dereference a void pointer then sizeof will return the size of the data type you cast the void pointer to. Pretty straight forward in it's own twisted way, but what's really interesting is when you dereference a void pointer to a char pointer you get the size of a char and not a char pointer. Here's a fun little test to see just what can happen:

double *d;
sizeof ( (char *) d ) will be the same as sizeof( char *)
because (char *) d considered as a rvalue is of type "char *",

and similarly
sizeof( ( * ( (char *) d) ) ) would be 1
as *( (char *) d ) is of type char considered as a rvalue

another point , I think in C89 the only portable way of printing sizeof is using %lu and casting the sizeof to (unsigned long)
The reason being that the result of sizeof is of type size_t , the only thing that is told about size_t is that it is an unsigned integral type , and since in C89 long is the largest integral type , there should be no problem with this .

printf( "sizeof(a)=%lu", (unsigned long) sizeof(a) ) ;
in C99 one can use %zd and no ugly casts required .

The only behavior of sizeof which might confuse some newbies is that if we have the declaration
char a[20];
then sizeof( a) will be 20 which most probably will be different from sizeof( (char *) ) , even here the general rule is not broken as a is not of type char * , but of type char [20]

Re: Re: Re: Re: abt VOID *

Originally posted by pinko_liberal Why do you misrepresent what I wrote ?

First off, I don't see you posting anything here. On that, I'll assume that you were "unregistered".

I didn't "misrepresent" what you wrote. I misread it, or at the time wasn't clear on what you were trying to get across.

My reply was not some personal attack on unknown. I was trying to clarify the issue.

You'll see how I could have possibly misunderstood your point, when your sentences come out like:

It does not do so because sizeof is not supposed to do so.

Case and point. This is not only an awkward statement, it's also untrue. Or rather, again it can be misinterpreted.

'sizeof' is supposed to return the size of anything passed to it. If you pass it a pointer, it returns the size of the pointer. If you pass an object, it returns the size of the object. That is all, nothing more.

What I was trying to clarify is that exact line of thought. And for the record, there is a type of 'void'. You just cannot create instances of it. If there weren't 'void', how could you use it as return values and parameters?

I want to say that all of you guys are really helpful and great. Thanx for sharing your ideas
and knowledge. I encourage you to debate in respect one another, but no intend for raising
a STAR WAR!
Back to the question I posted, can you guys show
how to access and dereference a VOID pointer?
No one has illustrate this point clearly so far!