473,768 Members | 3,519 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 #1
48 16962
Fr************@ googlemail.com wrote:
>
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 *?
The sole purpose of void*
is to reduce the amount of casts in a program.
Most pointer conversions to and from void* can be done without a cast.
Most other pointer conversions require a cast.

Before the invention of type void, char* was used instead.

--
pete
Mar 19 '07 #2
Fr************@ googlemail.com wrote:
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 *?
To avoid casting every assignment of something other than a char* to and
from a char*.

--
Ian Collins.
Mar 19 '07 #3
In article <11************ **********@n59g 2000hsh.googleg roups.com>,
<Fr************ @googlemail.com wrote:
>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 *?
char * means that it points to characters or bytes. void * doesn't;
it means that it points to something of unspecified type. Even if
there was no functional difference between them, it would be
worthwhile to be able to express your intent.

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 19 '07 #4
pete <pf*****@mindsp ring.comwrites:
Fr************@ googlemail.com wrote:
>>
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 *?

The sole purpose of void*
is to reduce the amount of casts in a program.
Most pointer conversions to and from void* can be done without a cast.
Most other pointer conversions require a cast.
No, that's not the *sole* purpose.

The problem with a char* is that you can't tell whether it's intended
to be a generic pointer (in pre-C89 code), or a pointer to actual
character data, or a pointer to a small integer (or an array thereof),
or a pointer to byte data (or an array thereof). The ANSI committee
invented void* to give us a generic pointer type, one that has the
advantage that you can't accidentally dereference it and obtain a
possibly meaningless char value.
Before the invention of type void, char* was used instead.
Yes.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 19 '07 #5
On Mar 19, 12:11 am, Ian Collins <ian-n...@hotmail.co mwrote:
To avoid casting every assignment of something other than a char* to and
from a char*.
I don't really follow this argument about minimizing number of casts.
For example, the following code fails - I still need to explicitly
cast p to a (struct s*) to avoid a compile-time error.

#include <stdio.h>

void call(void *);
struct s { int a; };

main()
{
struct s a;
a.a=1234;
call(&a);
return 0;
}

void call(void *p)
{
printf("%d\n",p->a);
}

--
Ian Collins.

Mar 19 '07 #6
Fr************@ googlemail.com wrote:
On Mar 19, 12:11 am, Ian Collins <ian-n...@hotmail.co mwrote:
>>To avoid casting every assignment of something other than a char* to and
from a char*.

I don't really follow this argument about minimizing number of casts.
For example, the following code fails - I still need to explicitly
cast p to a (struct s*) to avoid a compile-time error.
No, you don't.
#include <stdio.h>
Let's start by making it legal C:
void call(void *);
struct s { int a; };

main()
int main(void)
{
struct s a;
a.a=1234;
call(&a);
return 0;
}

void call(void *p)
{
printf("%d\n",p->a);
struct s *ps = p;
printf("%d\n",p s->a);
}
Now change call to

void call( char* );

And see what your compiler has to say. If you don't get two
incompatible type warnings, you haven't turned your warning level high
enough.

--
Ian Collins.
Mar 19 '07 #7
Flash Gordon wrote, On 19/03/07 10:10:

<snip>
> printf("%d\n",p->a);
struc s *sp = p;
A perfect example of why I should have copied and pasted even a small
example, not retyped on a different computer. That should have been
struct not struc, of course.
--
Flash Gordon
Mar 19 '07 #8
Fr************@ googlemail.com wrote, On 19/03/07 09:28:
On Mar 19, 12:11 am, Ian Collins <ian-n...@hotmail.co mwrote:
>To avoid casting every assignment of something other than a char* to and
from a char*.

I don't really follow this argument about minimizing number of casts.
For example, the following code fails - I still need to explicitly
cast p to a (struct s*) to avoid a compile-time error.

#include <stdio.h>

void call(void *);
struct s { int a; };

main()
{
struct s a;
a.a=1234;
call(&a);
No cast here that I can see, but if call took a char* as a parameter one
would be required.
return 0;
}

void call(void *p)
{
printf("%d\n",p->a);
struc s *sp = p;
printf("%d\n",s p->a);

Now it works since you are translating back to a compatible (the
original) type and not a cast in sight.
}
Also as a side note it would be better to be explicit about main
returning an int, especially as C99 has dropped implicit int. Best to be
specific about not taking parameters as well.
int main(void)
--
Flash Gordon
Mar 19 '07 #9
Fr************@ googlemail.com wrote:
>
On Mar 19, 12:11 am, Ian Collins <ian-n...@hotmail.co mwrote:
To avoid casting every assignment of something other than a char* to and
from a char*.

I don't really follow this argument about minimizing number of casts.
For example, the following code fails - I still need to explicitly
cast p to a (struct s*) to avoid a compile-time error.

#include <stdio.h>

void call(void *);
struct s { int a; };

main()
{
struct s a;
a.a=1234;
call(&a);
return 0;
}

void call(void *p)
{
printf("%d\n",p->a);
}
The problem is that you have used the (void *) type badly.

The prototype of the call function
void call(void *p);
indicates that it is intended
to take a pointer to any type object.
But it isn't intended to take a pointer to any type object.
The printf call statement, shows that p must be a pointer
to a struct with a member "a" of type int.
The right way to write that program is:

/* BEGIN new.c */

#include <stdio.h>

struct s {
int a;
};

void call(struct s* p);

int main(void)
{
struct s a;

a.a = 1234;
call(&a);
return 0;
}

void call(struct s *p)
{
printf("%d\n", p->a);
}

/* END new.c */

mem_cpy is an example of a function that uses
void * type parameters more better.

void *mem_cpy(void *s1, const void *s2, size_t n)
{
unsigned char *p1 = s1;
const unsigned char *p2 = s2;

while (n-- != 0) {
*p1++ = *p2++;
}
return s1;
}
--
pete
Mar 19 '07 #10

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
3185
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
3281
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
3665
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
10430
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
2290
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
5693
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
9578
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
9413
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
9973
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
8845
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...
0
6660
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
5292
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
5429
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3543
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
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.