473,406 Members | 2,343 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,406 software developers and data experts.

array of void*

Hi,

I am looking over some of my old implementations. One of them has an
"array" of void pointers defined as a pointer to the first element (without
getting stuck on nomenclature, I am aware of the difference between a
pointer and an array).

This pointer in effect is a pointer to a void pointer: void**. But iirc,
this is not entirely valid and may become a problem when dereferencing and
doing pointer arithmetic. And in all honesty, it would still all compile
fine if I wrote void* instead of void**. The "array" has to be dynamic in
nature, that is, I need to resize it (using realloc) and the data stored is
represented as a void* to allow different types of pointers to be stored.

What is the proper way of going about implementing this? Thanks for any
pointers and if I've missed anything in the faq, let me know.

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #1
3 20999
"Martijn" <su*********************@hotNOFILTERmail.com> wrote:
I am looking over some of my old implementations. One of them has an
"array" of void pointers defined as a pointer to the first element (without
getting stuck on nomenclature, I am aware of the difference between a
pointer and an array).

This pointer in effect is a pointer to a void pointer: void**. But iirc,
this is not entirely valid
It's quite valid.
and may become a problem when dereferencing and
doing pointer arithmetic.
Nope. A void * is an object. It is a pointer that does not point at any
kind of object, but the pointer itself is an object alright. Because of
this, a void ** is a perfectly normal object pointer, and doing pointer
arithmetic on one will work normally. For example, void_p_p++ will
increase void_p_p by sizeof(void *) bytes, just as int_p++ will increase
int_p by sizeof(int) bytes.
It is only void * _itself_ which cannot be used for pointer arithmetic,
because "a void object" has no size (and, AFAICT, doesn't actually
exist).
And in all honesty, it would still all compile
fine if I wrote void* instead of void**.


Yes, but that would be semantically incorrect, and might well invoke
undefined behaviour, depending on what your code actually does.

Richard
Nov 13 '05 #2
Thanks for clearing things up!

--
Martijn
http://www.sereneconcepts.nl
Nov 13 '05 #3
Martijn <su*********************@hotnofiltermail.com> wrote:
Hi,

I am looking over some of my old implementations. One of them has an
"array" of void pointers defined as a pointer to the first element (without
getting stuck on nomenclature, I am aware of the difference between a
pointer and an array).

This pointer in effect is a pointer to a void pointer: void**. But iirc,
this is not entirely valid and may become a problem when dereferencing and
doing pointer arithmetic. And in all honesty, it would still all compile
fine if I wrote void* instead of void**. The "array" has to be dynamic in
nature, that is, I need to resize it (using realloc) and the data stored is
represented as a void* to allow different types of pointers to be stored.

What is the proper way of going about implementing this? Thanks for any
pointers and if I've missed anything in the faq, let me know.


If you mean you have:

void **p = malloc(10 * sizeof *p);

and then you access the void * objects p[0] to p[9], then that's fine
and correct.

- Kevin.

Nov 13 '05 #4

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

Similar topics

7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
3
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
9
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
28
Nepomuk
by: Nepomuk | last post by:
Hi! I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work withsizeof(array)/sizeof(array)Now, this does work in some situations, but...
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?
0
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.