Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Question related to C programming. What is a void pointer?

Status
Not open for further replies.

ehsan_ullah

Newbie level 4
Joined
Apr 8, 2010
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Islamabad, Pakistan
Activity points
1,329
hi
i have the following questions
1) What is the void pointer? and when do we use it?
2) What is type casting and when do we use it?
 

Re: Question related to C programming. What is a void point

Dear ehsan_ullah,

a pointer variable normally points to something: for eg.,
int *p1;

'p1' points to an integer.

if you write
void *p2;

then ,

'p2' points to 'void'(nothing).

this is usually used to pass pointers in function parameters.

within the function u can cast it to any type u want.


suppose 'void *p2' is passed .
then within u can cast it as:

(int) *p2;

or whatever type reqd for ur application.

'void' pointers can be cast to any reqd type provide it matches ur application.

hope u got the point.
srizbf
23rdapril2010
 
Re: Question related to C programming. What is a void point

The void pointer is an aberration, and is used by incompetent hackers to produce completely unreadable and incomprehensible programs. :cry:

it does allow passing variable arguments of arbitrary types , again making code hard to read.
There is always a better way, using typedef, even if that type def must resolve to a void type.

Essentially a void pointer represents an absence of type, so at low level is simply a memory address without a size - though its usually 1 byte, the smallest memory type.
 

Re: Question related to C programming. What is a void point

Well, while I am all for typing (sort of) I don't think I'd say void pointers are aberrations. You might question yourself if you are using them in "general application code" thought. Here's my reasoning.

Look at the standard C library. You have malloc(). In the old days, malloc returned a char * (because we had no void * in those days). But the pointer is not really to char. It might be to a structure or a buffer or an array of integers or.... anything.

So what should malloc return? The library author has no idea. So using void * FORCES the programmer to think about what type s/he wants and then cast it.

Code:
struct xyz 
  {
   int a;
   int b;
   char tag[6];
   } *foo;

. . .
  char *p;
  p = malloc(sizeof(struct xyz));

An error checking compiler would pass this if malloc returned char *.

foo = malloc(sizeof(struct xyz));

An error checking compiler would fail this if malloc returne char *.

So returning void * is a good compromise. For one thing it can FORCE you to cast if you set the compiler so strict that it won't assume a void can be assigned to anything. Then both statements above are in error. Or you can relax the strictness to just warn you but do what you asked it to (which may or may not be the right thing) without allowing things like "p=foo" which is clearly wrong.

But if I am writing my application I should probably KNOW what things I want to do. I could build factories for each type:

Code:
struct xyz *make_anxyz(void) { return (struct xyz *)malloc(sizeof(struct xyz)); }
So I might agree that as an application programmer its kind of a hack to use a void pointer. I still might use it, though. But in a library it may be both necessary and helpful.

In your app it depends on how much you trade verboseness against type checking. So suppose I want to write a single function that could take a pointer to int or a pointer to float. Now in C++ I could override and that's nice. But in C I might write:

Code:
float average(int typeflag, void *numlist)
  {
   int *intlist;
   float *floatlist;
   float avg=0.0;
   int ct=0;
  if (typeflag==INTLIST) 
    {
    intlist=(int *)numlist;
    while (*intlist) 
      {
      ct++;
      avg+=(float)*intlist++;
      }
   }
 else if (typeflag==FLOATLIST)
   {
    floatlist=(float *)numlist;
    while (*floatlist)
      {
     ct++;
     avg+=*floatlist++;
     }
   }
   return avg/ct;
  }

But you could also write something like:

Code:
union list
  {
   int *ilist;
   float *flist;
   };


float average(int numtype, union list *nlist)
  {

But only because you know the types you want to handle before hand.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top