473,700 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

void pointers and more

Hi,

I am trying to get as much information on void pointers. How do we take help
of void pointers in writing generic functions.

I have seen this kind of code for many years, but now I have got a chance to
write some myself.

Could anybody point me to a link where I can get more info for the same, or
refer me a book having more detail info.

Also if anybody has read the boot "Practicalk programming in C"
Orielly...give me some idea of how the book is.
I plan to buy it today. :)

Thanks a lot.
Vivek
Nov 13 '05 #1
5 1965
In <vp************ @corp.supernews .com> "Vivek" <vi****@bmc.com > writes:
I am trying to get as much information on void pointers. How do we take help
of void pointers in writing generic functions.


We don't. They're only useful for functions expecting or returning
generic memory addresses. See the mem* functions from <string.h> and
malloc and friends.

It is fairly unusual to use them in other contexts: you can neither
dereference them nor perform arithmetic on them. Their sole merit is
that other pointer types are automatically converted to/from void
pointers when needed, without requiring a cast.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #2
On 2003-10-24, Dan Pop <Da*****@cern.c h> wrote:
In <vp************ @corp.supernews .com> "Vivek" <vi****@bmc.com > writes:
I am trying to get as much information on void pointers. How do we
take help of void pointers in writing generic functions.
We don't. They're only useful for functions expecting or returning
generic memory addresses. See the mem* functions from <string.h> and
malloc and friends.


Don't forget qsort and bsearch.
It is fairly unusual to use them in other contexts: you can neither
dereference them nor perform arithmetic on them. Their sole merit
is that other pointer types are automatically converted to/from void
pointers when needed, without requiring a cast.


This statement is accurate, but it does not imply that their use is
unusual. And while it may be their sole merit, it is useful in the
"generic" context if we take a guess at what the original poster means
by that term.

Functions like qsort and bsearch are generic, relying on size
information and a helper comparison function to take care of any thing
that would normally require specific type information.

In general, when designing a generic interface in C, the interface will
require the client of the interface to provide enough extra information
along with the pointer to do anything that requires type specific
information.

-- James
Nov 13 '05 #3
Dan Pop wrote:
"Vivek" <vi****@bmc.com > writes:
I am trying to get as much information on void pointers. How
do we take help of void pointers in writing generic functions.


We don't. They're only useful for functions expecting or
returning generic memory addresses. See the mem* functions from
<string.h> and malloc and friends.

It is fairly unusual to use them in other contexts: you can
neither dereference them nor perform arithmetic on them. Their
sole merit is that other pointer types are automatically
converted to/from void pointers when needed, without requiring
a cast.


What Dan fails to make clear above is their utility in allowing
pointers to be passed through code that does not care about the
actual type. The usage in the compare function passed through
qsort is typical. The caller of qsort, and the specified compare
function know what the actual pointer type is. All other routines
simply pass the pointers on.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 13 '05 #4
Any answers

"Vivek" <vi****@bmc.com > wrote in message
news:vp******** ****@corp.super news.com...
Hi,

I am trying to get as much information on void pointers. How do we take help of void pointers in writing generic functions.

I have seen this kind of code for many years, but now I have got a chance to write some myself.

Could anybody point me to a link where I can get more info for the same, or refer me a book having more detail info.

Also if anybody has read the boot "Practicalk programming in C"
Orielly...give me some idea of how the book is.
I plan to buy it today. :)

Thanks a lot.
Vivek

Nov 13 '05 #5
Vivek <vi****@bmc.com > scribbled the following:
Any answers


What's wrong with Dan Pop's, James Hu's and CBFalconer's answers? Do
you read this group or only write to it?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"As we all know, the hardware for the PC is great, but the software sucks."
- Petro Tyschtschenko
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
12730
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do exactly this, and similarly generic functions seem to have useful applications. In a few posts I read, it was suggested to avoid void pointer arithmetic by casting to a char pointer, and performing arithmetic on that (in multiples of...
8
448
by: John Hanley | last post by:
I working in C. I haven't paid much attention to void pointers in the past, but I am wondering if I can use them for my various linked lists to save work. I have two different linked lists that each use nodes of different structs. However the principle of my adding to the linked lists and removing them is the same. So will it work to have these functions accept a void pointer as an argument for the list and a void pointer as an...
188
17356
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
6349
by: Andreas Schmidt | last post by:
I am trying to understand the behavior of void pointers. Can someone explain to me why I get a segfault for the following program? #include <stdio.h> void* plusone(void* i){ int* arg = (int*)i; int result = (*arg + 1); return (void*)result; }
16
10384
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what do you mean by pointing to a void and why is it done? Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p; under different situations? I am kind of confused..can anybody clear this confusion by clearly...
27
8956
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in advance. Erik
49
2787
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code ;-): char *cp; void *vp; void **vpp;
4
1714
by: Oliver Block | last post by:
Hello, sometimes you can read that a function requiring an argument of tpye void ** is submitted something like the following: mystruct **ppval; myfunc((void **)&ppval, ...); I would expect that one would instead use
160
5638
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
1
8952
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
8911
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...
0
7794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5895
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
4395
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
4649
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.