i had seen in some places, the following piece of code:

(void *)2

Can anyone please tell me what exactly this means? What does casting a void pointer onto a constant as in the above case, signify?


6 Years
Discussion Span
Last Post by myk45

By itself it means absolutely nothing. In 16-bit compilers such as Turbo C it is sometimes necessary to hardcode memory addresses, such as the screen on MS-DOS is located at 0x8000 (or something like that). So it might be necessary to cast a pointer to that address char* screen = (void*)0x8000; Such addresses is not possible with 32 and 64 bit compilers.


void is a special type which is like a place holder for any type. void pointers are used as a kind of "incomplete cast" which allows you to store values without first specifying its type, but you must later specify its type by casting before using a void pointer. void pointers almost always point to something, and since they are just pointers, a pointer is usually the size of int. In some advanced cases, a void pointer is used as a temporary integer.

(void *) <-- that is a void pointer

(void *)2 <-- that is a void pointer to address 0x00000002

(void)2 <-- Error, cannot cast to a typeless type

int A;
(void *)&A
<-- This is how you convert the int pointer into a pointer of any other type

(void *)A; <-- This will point to the memory pointed to by the VALUE of int A.

So yeah, that's how you would use it. It might help if you give specific examples of (void *)2.

Edited by N1GHTS: n/a


Thanks for the replies.

Well, i saw this when i was going through threads(pThreads) from Advanced Programming in the Unix Environment(Richard Stevens)

This was the case:

/* Thread 1 */
void* fn_1(void *arg)
	/* Statements here */
	return ((void *)0);	/* This is what i was referring to */

/* Thread 2 */ 
void* fn_2(void *arg)
	/* statemennts here */ 

int main(void)
	/* Create the 2 threads */
	int err1 = pthread_create(&ntid, NULL, fn_1, NULL);
	int err2 = pthread_create(&ntid1, NULL, fn_2, NULL);
	/*-wait for threads to exit-*/
	void *val, *val1;
	pthread_join(ntid,  &val);
	pthread_join(ntid1, &val1);

	return 0;

So what exactly does it mean? (void *)0 in this case. Another example in the book says (void *)2.


((void *)0) is a special "command" that the C compiler looks for. Its completely different than ((void *)2) or anything else because the C compiler specifically will look for the ((void *)0) combination and does something special with it.

What it does is it looks at the context in which it's used, and then try's to create a pointer which points to something completely invalid for that context, which is usually memory position 0, but it could be something else, totally depends.

This is called a "null pointer". In the C standard library you might encounter a macro used quite often named NULL. If you look at its definition, its defined as ((void *)0). The null pointer will always be unique from any possible other pointer, even if there is a valid pointer to 0, in which case the compiler turns ((void *)0) into something crazy like 0xFFFFFFFF. Depends completely on what your context is.

If you want more information on null pointers, check this out:


In the example you gave, he was exploiting the null pointer feature.

Edited by N1GHTS: n/a


Thanks, i got it. So, basically (void *)0 is a null pointer. What about the (void *)2 ?
What does that signify?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.