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 sizeof(original _type)). Thus the pointer is cast through the sequence
original_type* -> void* -> char* -> void* -> original_type*.
I am quite sure this would work on many implementations (where the casts are
no-ops), but the threads I saw containing the above suggestion did not make
clear whether or not it was undefined (or implementation defined)
behaviour - is it? If so, is there any way to achieve the desired effect
while strictly conforming to the standards?
TIA,
Alex
Nov 13 '05
22 12739
Jeremy Yallop wrote: I think this needs to be something like:
int compare(const void *s1, const void *s2) { return strcmp(*(char **)s1 + OFFSET, *(char **)s2 + OFFSET); }
Or
int compare(const void *s1, const void *s2)
{
return strcmp(*(char *const *)s1 + OFFSET,
*(char *const *)s2 + OFFSET);
}
If you want to avoid some compilers' warnings about casting
away 'const'.
--
Hallvard
Hallvard B Furuseth wrote: Jeremy Yallop wrote:
I think this needs to be something like:
int compare(const void *s1, const void *s2) { return strcmp(*(char **)s1 + OFFSET, *(char **)s2 + OFFSET); } Or
int compare(const void *s1, const void *s2) { return strcmp(*(char *const *)s1 + OFFSET, *(char *const *)s2 + OFFSET); }
Sure. I think that counts as "something like". In this case I find
that the extra "const" makes the code more difficult to read. I might
add it if using extra local variables instead of casting.
If you want to avoid some compilers' warnings about casting away 'const'.
There should be a way to turn that warning off without changing the
code. Casting away const is sometimes necessary (e.g. to implement a
function with an interface like strstr()). (Okay, not strictly
/necessary/ - you can use memcpy() instead, for example - but
"necessary in sane code").
Jeremy.
CBFalconer wrote: Jeremy Yallop wrote: Hallvard B Furuseth wrote: > ... snip ... > > int compare(const void *s1, const void *s2) > { > return strcmp(*(char *const *)s1 + OFFSET, > *(char *const *)s2 + OFFSET); > }
Sure. I think that counts as "something like". In this case I find that the extra "const" makes the code more difficult to read. I might add it if using extra local variables instead of casting.
> If you want to avoid some compilers' warnings about casting > away 'const'.
There should be a way to turn that warning off without changing the code. Casting away const is sometimes necessary (e.g. to implement a function with an interface like strstr()). (Okay, not strictly /necessary/ - you can use memcpy() instead, for example - but "necessary in sane code").
/* invalid for strlen(s) < OFFSET */ int compare(const void *s1, const void *s2) { const char *p1 = s1; const char *p2 = s2;
return strcmp(p1 + OFFSET, p2 + OFFSET); }
No, this is wrong. You need an extra level of indirection. The
pointers passed to the comparison function are pointers to the objects
in the array. That is, they're pointers to pointers to char disguised
as pointers to (const) void. If you want to write the comparison
function in this way you can write:
int compare(const void *s1, const void *s2)
{
const char *const *p1 = s1;
const char *const *p2 = s2;
return strcmp(*p1 + OFFSET, *p2 + OFFSET);
}
(with more or fewer "const"s according to taste).
Jeremy. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Xavier Decoret |
last post by:
This is a compiler error that I get when I try to do:
void* p;
unsigned int stride;
// ...
p += stride;
I have good reason to do this, namely an iterator over an array of
object whose type may vary, as outlined below. So I don't understand why
|
by: Martijn |
last post by:
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,...
|
by: Xiangliang Meng |
last post by:
Hi, all.
As far as I know, the speical arithmetic operator on void pointer is an
externsion in GNU CC. However, I could not find the relative topics in C99.
Would someone like to help me find them out?
in linux\mm\Slab.c,
typedef struct slab_s {
struct list_head list;
unsigned long colouroff;
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
|
by: Alfonso Morra |
last post by:
Hi,
I'm getting a compiler error of "pointer truncation from 'void *' to
'int'" because I'm not explicitly casting from void* to (datatype*).
Pointers are stored as ints (is that right?), so as AFAIK, there is
nothing to worry about is there?
I'm using VC 7.1
Why are such assignments being treated as errors?
| |
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
|
by: arun |
last post by:
Hi,
Why sizeof operator when applied on void returns one when compiled
with gcc compiler ??. When i tried it on VC++ compiler, it gives an
error. But another version of the VC++ compiler on my friend's machine
gives it as zero. Have anyone tried this ? I believe it should give an
error because i think there is nothing called void.
Regards,
arun..
|
by: Angus |
last post by:
I am using a global which is a void*
I have it defined in one file as:
void* hFlag;
and one other header file as:
extern void* hFlag;
But I get this compile error:
|
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;
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |