473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Generic Pointer

I'am suffering with the concept of generic (void) pointer in C++. Please any body tell me the concept of generic pointer...
Nov 30 '07 #1
2 4266
671 Expert 512MB
Is there something specific about it that you don't understand? Perhaps you were reading an online reference, and didn't understand part of the description? Pointing out those specifics makes it easier for us to help you out.

With C and C++, everything must have a type. You can't say "I have a variable", and not be able to say if it is a char or int or a more complex structure. Now, with the aid of pointers, you can refer to variables. The problem is when you want a way of referring to something generically. For example, if you have a function that uses an argument. Your function deals with chars and ints differently. You just want the function to be able to accept something, which you'll resolve to a char or int yourself. This ambiguous something is the void pointer.

Void pointers are used heavily in C. Not so in C++, where there are syntactical constructs for generics, like function overloading and templates.
Nov 30 '07 #2
9,208 Expert Mod 8TB
What happened was:

In 1972 C was invented. In C function names must be unique so if you wrote a function to display an int:
Expand|Select|Wrap|Line Numbers
  1. void display(int arg)
  2. {
  3.      printf("%d", arg);
  4. }
then no one could use the name display for a funciton to display a float.

That forced function design to be not specific about the data and that caused the function to be written with a pointer to the data. A pointer to data whose type is not known is a void*. That is, it's a generic address. Now the function looks like:
Expand|Select|Wrap|Line Numbers
  1. void display(void* arg)
  2. {
  3.      printf("%d", *arg);
  4. }
which won't compile since you cannot dereference an address unless the address has a type. Like, int* dereferences to int so a void* would have to dereference to a void and a void means no type. Since all data must have type, the result is you are not allowed to dereference a void pointer.

Now you have to tell the function what the tyoe of the argument is by using a second argument. The function can then typecast the void pointer:
Expand|Select|Wrap|Line Numbers
  1. void display(void* arg, int parm)
  2.      int* temp_int;   
  3.      float* temp_float;
  4.      switch(parm)
  5.      {
  6.        case 1:
  7.            temp_int = (int*)arg;            
  8.            printf("%d", *temp_int);
  9.            break;
  10.       case 2:
  11.            temp_float = (float*)arg;
  12.            printf("%f", *temp_float);
  13.            break;
  14.        case 3:
  15.            /* you get the idea */
  17. }
This is really ugly and it forces you to change the function each time you have a new type of data to display. If the function has been distributed tio customers, then you have to upgrade all those customers.

C++ solves this problem with function overloading. Here the function name and the arguments must be unique rather than just the name.

oler1s says void* are not heavily used in C++. I would like to strengthen that by saying void* is not to be used in C++. void* is in C++ largely to be compatile with C and to allow you to use C functions if you really have to.
Nov 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
by: Albert | last post by:
Hello all, I've got several questions and I'm sure you'll find them easy to answer, but sadly I don't know the answers though: What do people mean by generic pointers Why is a pointer to void...
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
by: Frederick Gotham | last post by:
For objects, we have "void*" as the generic pointer type. For instance: enum ParamType { Int, Double }; void Func(void *const p,ParamType const pt) { switch (pt) { case Int: *(int*)p = 42;...
by: palani12kumar | last post by:
Can any one tell me what is generic poiner? im struggling a lot to know what it is?
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
by: software | last post by:
Hello, I am a new bee to c++/cli, here's my question: I have a template version of a smart pointer class I can't use in my application because I need to use it in more than one assembly. I...
by: juerg.lemke | last post by:
Hi everyone I am interested in having multiple functions with different prototypes and deciding, by setting a pointer, which of them to use later in the program. Eg: int f1(void); char*...
by: Nadeem Afroz | last post by:
Hello Folks!!!! i have one interesting question over here .......... Is it possible to create a generic pointer (generic pointer to member functions) which can point to all the member...
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.