473,915 Members | 4,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic Pointer

5 New Member
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 4281
oler1s
671 Recognized Expert Contributor
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 Recognized Expert Moderator Expert
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
4473
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. TIA #include <stdio.h>
1
1926
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 generic Why would someone need pointers to void? Thanks
11
4734
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 some more research, that what I really need is known in C as a "dynamic array". Basically, you surpass the array notation and use pointers with memory obtained with malloc() or calloc(). I think this will do just what I needed. That has brought...
3
1758
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; break; case Double: *(double*)p = 42; break;
4
15868
by: palani12kumar | last post by:
Can any one tell me what is generic poiner? im struggling a lot to know what it is?
1
3438
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 it. Therefore I wanted to creat a class member which represents the pointer to the array. Unfortunately I get the folowring compile error for the code beneath: error C3229: 'DataType *' : indirections on a generic type parameter
2
1522
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 thought of rewriting the class as a generic class to avoid this problem. Unfortuneatly it is not possible to use Pointers inside generic classes. Code:
7
3389
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* f2(int);
2
2301
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 functions of a class Ex : class abc() { int fun1(int a)
32
5696
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 and lccwin32 for example) which I consider to be perfectly reasonable: you can cast every kind of function pointer to a void pointer and void pointers to any kind of function pointer. This follows the general "generics through void scheme" of C....
0
10039
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11354
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10923
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5943
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.