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

something to do with void *

hello, this is a piece of code ,which is giving an error.
#include<stdio.h>

int main()
{
int a =10;
void *p = &a;
printf("%d ", *p ); //error....why should it //be an error ?can't the
compiler make out because //of %d that the pointer is supposed to refer to
an integer ?? or is explicit type casting required ??
++p ; //agree that this is an error.
return 0;
}

plssss tell me about the first error.thanx.
ranjan.

Nov 15 '05
56 3253
On Thu, 04 Aug 2005 01:14:30 -0700, Tim Rentsch wrote:
"Netocrat" <ne******@dodo.com.au> writes: <snip>
I don't see any reason for void to be/have a value.

.... It's a little nicer, and just as consistent, to think of
void as having a single value that, because it's always the
same, takes zero bits to store, and is completely optimized
in both space and run-time by all C compilers. <snip>

OK void could be defined as a type with a single unique empty value as
you and Chris describe, but it seems contrary to the purpose of void,
which is to represent an expression that does something (has side effects)
but doesn't result in a direct value. To me the value of void as a value
is valueless.
There could be a problem with
void arrays, since the usual calculation for number of
elements ('sizeof x / sizeof *x') doesn't work if 'sizeof x'
is zero, but that minor difficulty can easily be gotten
around.

Somewhat tongue-in-cheek, but only somewhat.


I don't even want to contemplate Chuck's reaction to a discussion of void
arrays. ;)

Nov 15 '05 #51
Tim Rentsch <tx*@alumnus.caltech.edu> wrote:
"S.Tobias" <si***@FamOuS.BedBuG.pAlS.INVALID> writes:
Tim Rentsch <tx*@alumnus.caltech.edu> wrote:
> For example,
> 'void' functions in C++ are allowed to 'return' void
> expressions, to simplify certains kinds of automatic code
> generation;


[OT] I think you're wrong, both in C and in C++ `return' works the same.
I miss this feature, too.


I double checked via a Google search, and turned up these
pages (among others):

http://www.ittips.com/computersindex_v2-103-1.htm
http://www.edg.com/cpp_ftrs.html

So it seems like the latest version of the C++ standard
does support 'return' with void expressions for 'void'
functions.


Thanks, I wasn't aware of that, you're right. I've looked into
the C++03 Std now, and it agrees with you.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #52
Netocrat <ne******@dodo.com.au> writes:
On Thu, 04 Aug 2005 01:14:30 -0700, Tim Rentsch wrote:
"Netocrat" <ne******@dodo.com.au> writes:

<snip>
I don't see any reason for void to be/have a value.

...
It's a little nicer, and just as consistent, to think of
void as having a single value that, because it's always the
same, takes zero bits to store, and is completely optimized
in both space and run-time by all C compilers.

<snip>

OK void could be defined as a type with a single unique empty value as
you and Chris describe, but it seems contrary to the purpose of void,
which is to represent an expression that does something (has side effects)
but doesn't result in a direct value. To me the value of void as a value
is valueless.


Rather than thinking of void as a type that means "no value"
you might think of void as a type whose sole value is "empty".
The empty set is still a set, even though it is empty.

The value of a void expression, because it is "worthless", is
discarded.

Again, somewhat tongue in cheek. However, I think this view
is equally consistent, and it could simplify treatment of void
expresssions.

Nov 15 '05 #53
Netocrat <ne******@dodo.com.au> wrote:
On Thu, 04 Aug 2005 08:38:28 +0000, S.Tobias wrote:
Netocrat <ne******@dodo.com.au> wrote:

<snip>
I think that "unknown" is a more appropriate type for
this pointer. Pointing to an object of unknown type makes more sense
than pointing to an object of a type not allowed to be an object.


I think "unknown" is the word for type `void'. If C had an `unknown'
type, would it be in any way different than `void'?


Its definition would limit it solely to use as a pointed-to type for a
generic pointer to any object. Dereferencing such a pointer would not
be possible (the pointed-to type is unknown).


Equivalent solution would be to disallow dereferencing `void*' (while
still allowing void expressions).

Another way to look at `void' is to call it "empty", and apply
it to whatever fits that emptiness (void expressions, which represent
"Nothing", and pointers to "Unknown" which can't point to any concrete
value).

Perhaps the name "void" is is a little bit "overloaded" in that in `void*'
it means a slightly different thing than in plain `void'.
Personally, I'm quite happy with the simple way it is now and I wouldn't
welcome any changes there.

void *obj = malloc(19);
We have obj pointing to an object of void type. We have *obj an
lvalue.
I don't believe it's intended to be specified as an lvalue by the
standard, although as Stan Tobias argues it may unintendedly be.

I don't remember doing that.

Must be a case of identity theft then. Someone recently started an entire
thread devoted to the issue on comp.std.c in your name.


:-)
Oh, you mean that one... But I strongly believe that void expressions
aren't lvalues and should never be, even if the Std says otherwise. ;-)

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #54
Netocrat wrote:
Chris Dollin wrote:
Netocrat wrote:
Chris Dollin wrote:
> Netocrat wrote:
> > On Tue, 02 Aug 2005 14:39:57 +0100, Chris Dollin wrote:
> >> Netocrat wrote: *snip*> >>> Given that a void type can't
> >>> have a value, the use of the term "expression" seems appropriate.
> >>
> >> `Expression` *cannot* be right; expressions are things that the
> >> grammar describes and the compiler handles, but they don't have to
> >> exist at run-time, when evaluation takes place.
> >
> > Is it evaluated?
>
> Is there something in the Standard that prevents it?

6.3.2.2#1: "The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way [...] (A void
expression is evaluated for its side effects.)"


So it is evaluated,


There are no side effects to dereferencing a void pointer.


What if it is volatile?
Nov 15 '05 #55
On Sat, 06 Aug 2005 20:41:44 +0000, wrote:
Netocrat wrote:
Chris Dollin wrote:
> Netocrat wrote:
>
> > Chris Dollin wrote:
> >> Netocrat wrote:
> >> > On Tue, 02 Aug 2005 14:39:57 +0100, Chris Dollin wrote:
> >> >> Netocrat wrote: *snip* > >> >>> Given that a void type can't
> >> >>> have a value, the use of the term "expression" seems appropriate.
> >> >>
> >> >> `Expression` *cannot* be right; expressions are things that the
> >> >> grammar describes and the compiler handles, but they don't have to
> >> >> exist at run-time, when evaluation takes place.
> >> >
> >> > Is it evaluated?
> >>
> >> Is there something in the Standard that prevents it?
> >
> > 6.3.2.2#1: "The (nonexistent) value of a void expression (an expression > > that has type void) shall not be used in any way [...] (A void
> > expression is evaluated for its side effects.)"
>
> So it is evaluated,


There are no side effects to dereferencing a void pointer.


What if it is volatile?


Still not a side effect. The pointer does not reference an object with a
value, so there is nothing that may have changed.

Nov 15 '05 #56
On Sat, 06 Aug 2005 17:32:37 +0000, S.Tobias wrote:
Perhaps the name "void" is is a little bit "overloaded" in that in
`void*' it means a slightly different thing than in plain `void'.
That's the issue exactly.
Personally, I'm quite happy with the simple way it is now and I wouldn't
welcome any changes there.


Oh I'm not seriously advocating a new type - it all works fine in
practice. I'm just pointing out that as you seem to agree there's a
little conceptual inconsistency in the model. It would be cleared up
by not using void* for an unknown pointer.

Nov 15 '05 #57

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

Similar topics

3
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
9
by: Igor Okulist | last post by:
int func(void**); { short* p = NULL; func(&p); //<<< here } Could somebody remind me why is this not allowed ? error message: "cannot convert parameter from 'short **' to 'void **'"
9
by: Alex Vinokur | last post by:
Is this approach safe? class Foo { // Stuff }; void func1 (void *) { // Do something
4
by: brianhray | last post by:
Hello: I am writing a C interface and was curious how/why a void* can be used as a reference parameter. //////////////////////////////////////////////////////////// // WORKS: // Client1.h...
18
by: hyderabadblues | last post by:
What does (void) poService in followinf peace of code mean tclCtrlBoard::tclCtrlBoard( void* poService ) { # if defined Something (void) poService; \\ What does this mean }
35
by: Roman Mashak | last post by:
Hello, I want to adapt my linked list implementation to hold any data types. As I understand it's a good place to use 'void *'. Here's what I've done: struct list_node { void *data; struct...
8
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs...
160
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;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.