473,385 Members | 1,492 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,385 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 4265
oler1s
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
weaknessforcats
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. }
  5.  
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. }
  5.  
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 */
  16.  
  17. }
  18.  
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

6
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....
1
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...
11
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...
3
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;...
4
by: palani12kumar | last post by:
Can any one tell me what is generic poiner? im struggling a lot to know what it is?
1
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...
2
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...
7
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*...
2
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...
32
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.