473,775 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

void * vs char *

I've been reflecting on these two types of pointer. As far as I can
glean from books, void * and char * are functionally equivalent: the
key property of both is that they are pointers that can be faithfully
cast to any other pointer type.

The only difference seems to be that it is legal to perform arithmetic
on a char *, but not on a void *.

So if a char * really is just a more functional void *, why would
anyone ever use a void * in their code, instead of a char *?

Mar 19 '07
48 16973
On Mar 19, 8:58 pm, Ian Collins <ian-n...@hotmail.co mwrote:
The whole point of that example was that it was code that used a void
* but wouldn't compile unless you added in an explicit cast.

But it didn't!
Didn't what?
*Please* don't quote signatures.
Sorry - I'll try to be careful with that in future.

Mar 19 '07 #21
just an example to illustrate a point, proof-of-context if you like.

Oops meant concept of course.

Mar 19 '07 #22
On Mar 19, 7:33 pm, Keith Thompson <k...@mib.orgwr ote:
You seem to be arguing that the compiler should shut up about implicit
conversions that don't lose any information; in other words, you want
C's type checking to be even weaker than it is. You're certainly
entitled to that opinion, but it's not one many people share. If you
want a generic pointer type, use void*. There are plenty of
mechanisms that let you be sloppy with types if you do it
deliberately; *I* want the compiler to warn me if I do it
accidentally.
That's an interesting way to put it. Maybe that's what's happening -
I've been forced to use Java for the last couple of years, which has
this horrible over-bearing strict typing, so maybe I'm reacting
against that now and trying to push the liberating flexibility that C
gives me...

However, I'm not completely sure it should be the job of the compiler
and not some optional lint tool to pick through my code for places
where I might just have meant something else.

Mar 19 '07 #23
Fr************@ googlemail.com wrote:
On Mar 19, 8:58 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>>>The whole point of that example was that it was code that used a void
* but wouldn't compile unless you added in an explicit cast.

But it didn't!


Didn't what?
Require an explicit cast. I used implicit conversion from void*.

--
Ian Collins.
Mar 19 '07 #24
Fr************@ googlemail.com said:
On Mar 19, 7:59 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>Francine.Ne... @googlemail.com said:
The whole point of that example was that it was code that used a
void * but wouldn't compile unless you added in an explicit cast.

But it was lousy code. Sorry, but it was. That simply isn't the kind
of place where void * is a win.

Ease up there! I wasn't submitting it to a good code contest - it was
just an example to illustrate a point, proof-of-context if you like.
Sure, but the point is that it doesn't illustrate your point well,
because it's like saying "screwdrive rs are a waste of time - I tried
hammering a nail in with one and hey, I might as well have used a
hammer". Your illustration did not provide a reason not to use void *.
It just showed that there are circumstances where void * may not be
appropriate - and we already knew that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 19 '07 #25
On Mar 19, 9:34 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Francine.Ne...@ googlemail.com said:
On Mar 19, 7:59 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Francine.Ne...@ googlemail.com said:
The whole point of that example was that it was code that used a
void * but wouldn't compile unless you added in an explicit cast.
But it was lousy code. Sorry, but it was. That simply isn't the kind
of place where void * is a win.
Ease up there! I wasn't submitting it to a good code contest - it was
just an example to illustrate a point, proof-of-context if you like.

Sure, but the point is that it doesn't illustrate your point well,
because it's like saying "screwdrive rs are a waste of time - I tried
hammering a nail in with one and hey, I might as well have used a
hammer". Your illustration did not provide a reason not to use void *.
It just showed that there are circumstances where void * may not be
appropriate - and we already knew that.
Maybe you're right! I guess the issue I have is that I'm not really
convinced that void * should exist at all. I mean, char * can double
up perfectly well as both a pointer to char and a "generic pointer",
but for some reason if you work with char * you need to do a lot of
extra explicit casting. At the very least, there seems to be a lot of
duplication between the function of void * and char *, whereas I
always think minimality and efficiency is a virtue.

Mar 19 '07 #26
On Mar 19, 9:27 pm, Ian Collins <ian-n...@hotmail.co mwrote:
Francine.Ne...@ googlemail.com wrote:
On Mar 19, 8:58 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>>The whole point of that example was that it was code that used a void
* but wouldn't compile unless you added in an explicit cast.
>But it didn't!
Didn't what?

Require an explicit cast. I used implicit conversion from void*.
Well, that's true, though the net result was you saved a pair of
parentheses in your source code, and in exchange picked up an extra
pointer to carry round!

Mar 19 '07 #27
Fr************@ googlemail.com wrote:
On Mar 19, 9:34 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>
Sure, but the point is that it doesn't illustrate your point well,
because it's like saying "screwdrive rs are a waste of time - I tried
hammering a nail in with one and hey, I might as well have used a
hammer". Your illustration did not provide a reason not to use void *.
It just showed that there are circumstances where void * may not be
appropriate - and we already knew that.

Maybe you're right! I guess the issue I have is that I'm not really
convinced that void * should exist at all. I mean, char * can double
up perfectly well as both a pointer to char and a "generic pointer",
but for some reason if you work with char * you need to do a lot of
extra explicit casting. At the very least, there seems to be a lot of
duplication between the function of void * and char *, whereas I
always think minimality and efficiency is a virtue.
No, void* serves as a generic pointer, to have char* double up (and
don't forget it used to, before void* was added to the language) adds
complexity.

void foo( char* );

Does foo() expect an generic pointer, or does foo() only expect a char*?
void* removes the ambiguity and adds clarity.

Efficiency is completely irrelevant.

--
Ian Collins.
Mar 19 '07 #28
Fr************@ googlemail.com wrote:
On Mar 19, 9:27 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>>Francine.Ne.. .@googlemail.co m wrote:
>>>On Mar 19, 8:58 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>>>>>The whole point of that example was that it was code that used a void
>* but wouldn't compile unless you added in an explicit cast.
>>>>But it didn't!
>>>Didn't what?

Require an explicit cast. I used implicit conversion from void*.

Well, that's true, though the net result was you saved a pair of
parentheses in your source code, and in exchange picked up an extra
pointer to carry round!
The net result was I was able to use implicit conversion from void* and
didn't have to resort to (explicit, as all casts are) casting.

I'm not sure where you get the issue with the temporary pointer from.
It's just that, a temporary variable. In the trivial example you posted
it is used exactly once, in a more complex case, it would be used
wherever the passed parameter is dereferenced.

--
Ian Collins.
Mar 19 '07 #29
On 19 Mar 2007 14:13:36 -0700, Fr************@ googlemail.com wrote:
>On Mar 19, 7:33 pm, Keith Thompson <k...@mib.orgwr ote:
>You seem to be arguing that the compiler should shut up about implicit
conversions that don't lose any information; in other words, you want
C's type checking to be even weaker than it is. You're certainly
entitled to that opinion, but it's not one many people share. If you
want a generic pointer type, use void*. There are plenty of
mechanisms that let you be sloppy with types if you do it
deliberately ; *I* want the compiler to warn me if I do it
accidentally .

That's an interesting way to put it. Maybe that's what's happening -
I've been forced to use Java for the last couple of years, which has
this horrible over-bearing strict typing, so maybe I'm reacting
against that now and trying to push the liberating flexibility that C
gives me...

However, I'm not completely sure it should be the job of the compiler
and not some optional lint tool to pick through my code for places
where I might just have meant something else.
With most compilers, the "linting" level is controllable. Find a set
of switches you like. However, keep in mind that compilers actually
aren't always smart enough to distinguish warnings from errors.

--
Al Balmer
Sun City, AZ
Mar 19 '07 #30

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

Similar topics

4
2547
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
1
2326
by: Steffen Fiksdal | last post by:
I have a function that base64 decodes some data. The incoming data is received as "const char*" (BASE64 characters are always safe ASCII characters, meaning they will always fit in a signed char positive range). The resulting decoded data is placed in memory, and the function exposes an "unsigned char*" to the caller. What does ANSI C say (if anything) about what kind of pointer is the correct one to use for...
33
3187
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
1
3283
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming -- Deep Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line 10 is illegal? Is what Keith Thompson said in another post also helpful to understand this question: "... The implicit conversion rule applies only to void*, not...
33
3667
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of serializing the object array). I can think of three ways to do this:
18
10433
by: planetzoom | last post by:
Given the following code: #include <stdio.h> int main(void) { char array = "What is your favorite car?"; void *vp = &array; printf("%s\n", vp);
17
2292
by: mdh | last post by:
In trying to understand the issue, I wrote this; #include <stdio.h> void f_output(char arg1, int limit); int main () { f(); return 0; }
160
5712
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;
28
1824
by: junky_fellow | last post by:
Guys, Consider a function func(void **var) { /* In function I need to typecast the variable var as (int **) I mean to say, I need to access var as (int **) }
4
5070
by: Amera | last post by:
hello , I have written these codes : Mydll file : Mydll.h #ifndef MYDLL_H
0
9625
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
9459
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,...
0
9920
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...
1
7466
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...
0
6721
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
5365
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
5489
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2857
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.