473,569 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 21019
"Martijn" <su************ *********@hotNO FILTERmail.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************ *********@hotno filtermail.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
25153
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 (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
3
2464
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 am trying to calculate, with each sort, how many times during the sort the array elements are compared and swapped.
5
3504
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 accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can...
12
112081
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
2981
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: #include <stdlib.h> #ifndef __LIST_H__ #define __LIST_H__
24
3413
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
9
5599
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const void *keyA,const void *keyB));
23
7371
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion....
14
20377
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 passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case...
28
4549
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 not in others. Here's what I mean:#include <iostream> int length(int * array){return sizeof(array)/sizeof(array);} int main() { int array1 =...
0
7703
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...
0
7926
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. ...
1
7678
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...
0
6286
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...
0
5222
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
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
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
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...

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.