LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2005, 04:07 PM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
meaning of void*


What does "void*" mean in c code? I have only used java and in general i understand pointers, but i cannot understand this.
First this can be used both as a return value and as a parameter for a function. Can it?
When do we use it? Reading some code, i have only associated it with allocated part/s of memory Can somebody explain? A couple of examples maybe?
Thanks
 
Old 10-12-2005, 04:27 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You use it when you want a pointer of an unspecified type. For instance, malloc() returns a void * because it has no idea what you're going to assign that pointer to. You might want it assigned to an int *, or a char *, or even a struct my_struct *.

Yes, you can also use it as a parameter to a function. Say you want to create a function that can add ints or floats. You could make it something like this:
Code:
void *sum(void *a, void *b, int is_flt)
{
  if(is_flt)
    return (*(float *)a) + (*(float *)b);
  else
    return (*(int *)a) + (*(int *)b);
}
Then you can call it something like:
Code:
int a = 5, b = 7;

sum(&a, &b, 0);
And likewise for floats except the 3rd argument would be 1 instead of 0.
 
Old 10-12-2005, 04:39 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: meaning of void*

Quote:
Originally posted by nocturna_gr
What does "void*" mean in c code?
It means "pointer to no-specific-type".

Quote:
First this can be used both as a return value and as a parameter for a function. Can it?
That's correct. If it's used as a return value, the function returns a pointer to some block of memory, while not stating of which type it is. It can be used for chars, int's, binary data,..

Quote:
When do we use it? Reading some code, i have only associated it with allocated part/s of memory
Often it's known of which type the pointer we are dealing with and we don't use void*. But consider for example malloc() that talks to the kernel to get (allocate) a block of memory for your program. It's of course impossible for malloc() to know in advance for which type of variable you are going to use it. So malloc() returns a non-type-specific pointer: void* (see man malloc).

Sometimes it can be handy to assign a type (cast) such a block of code. E.g. if you are going to store int's in it because int's are 4 bytes. When you have a pointer int *idx it will advanced 4 bytes when you do idx++, and 12 bytes if you do idx = idx + 3.

To assign a type int to a block of memory returned by malloc():
Code:
int *intblockpointer;
int *idx;

intblockpointer = (int*)malloc(1000 * sizeof(int));

/* Find the 0 */
idx = intblockpointer;
while (*idx <> 0) {
    idx++ /* Actually advances 4 bytes (sizeof(int) == 4) */
}
free(intblockpointer);
Note: this isn't good code, it's just a short example.
It's not good because if it doesn't find a 0-int, the idx pointer goed wild.
 
Old 10-12-2005, 04:48 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Since you say you understand Java, you can also think of a void* as something similar to Java's object class. Everything in Java inherits from the object class, so you can use that as a parameter to functions that you want to be able to take anything.

However in C++ there is no one class that all other classes derive from like this. And in C, you don't even have classes or inheritance. So, as has already been stated, you can use void* to get a pointer to any type you want.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
void pointers suchi_s Programming 9 11-08-2004 03:05 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM
void? Patchorus Programming 9 10-25-2003 07:24 PM
void pointer help gonnaWorkItOut Programming 1 10-12-2003 11:52 AM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration