473,750 Members | 2,213 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 1969
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
12738
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
17406
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
6352
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
10392
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
8968
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
2800
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
1717
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
5661
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;
0
9000
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
8838
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9339
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
9256
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
8260
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...
1
6804
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...
1
3322
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
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
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.