473,739 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 #1
56 3345
maadhuu wrote:
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 is illegal because you are not permitted to dereference a void
pointer. That you clearly "intended" *p to be an int doesn't permit
the compiler to ignore the error.

If *someVoidPointe r *were* legal, the result would be the unique
value of the void type [1], which isn't an int either.

[1] No, I don't subscribe to the nonsense that `void` is an empty type.

--
Chris "electric hedgehog" Dollin
predicting self-predictors' predictions is predictably unpredictable.
Nov 15 '05 #2
Chris Dollin wrote:
maadhuu wrote:
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 is illegal because you are not permitted to dereference a void
pointer. That you clearly "intended" *p to be an int doesn't permit
the compiler to ignore the error.


In this case the printf statement is incorrect, but simply
dereferencing a valid void pointer is not illegal.
If *someVoidPointe r *were* legal
Which it is...
the result would be the unique
value of the void type [1], which isn't an int either.
It yields an expression of type void. Not sure what you mean by unique
value of void type.
[1] No, I don't subscribe to the nonsense that `void` is an empty type.


Oh, not one of those suckers who blindly subscribes to the Standard?

6.2.5p19:
"The void type comprises an empty set of values; it is an incomplete
type that cannot be completed."

Robert Gamble

Nov 15 '05 #3
maadhuu <ma************ @yahoo.com> wrote:
int main()
{
int a =10;
void *p = &a;
printf("%d ", *p );
can't the compiler make out because of %d that the pointer is supposed
to refer to an integer?
No.
++p; //agree that this is an error.
The same principle applies; if the compiler knew what p pointed at,
then both this and *p would be acceptable.
or is explicit type casting required ??


Yes.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #4
Robert Gamble wrote:
Chris Dollin wrote:
maadhuu wrote:
> 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 is illegal because you are not permitted to dereference a void
pointer. That you clearly "intended" *p to be an int doesn't permit
the compiler to ignore the error.


In this case the printf statement is incorrect, but simply
dereferencing a valid void pointer is not illegal.
If *someVoidPointe r *were* legal


Which it is...


I beg to differ. It is not legal in standard C. Your very own quote
below says that `void` is an incomplete type, and incomplete types
cannot be dereferenced when I last looked.
the result would be the unique
value of the void type [1], which isn't an int either.


It yields an expression of type void. Not sure what you mean by unique
value of void type.


Evaluating an expression of type T returns a result which is an
instance of T -- eg, `1+2` is an expression of type `int` who's
result is the value `3`, an instance of `int`.

The unique value of type `void` is the single, unique value which
is of type `void`.
[1] No, I don't subscribe to the nonsense that `void` is an empty type.


Oh, not one of those suckers who blindly subscribes to the Standard?


That's right.
6.2.5p19:
"The void type comprises an empty set of values; it is an incomplete
type that cannot be completed."


Yes, I know. That's why I said that I didn't subscribe to that
nonsense; because I don't. The view that `void` is an empty type
leads to unfortunate conclusions about the existance of functions
returning void, which are trivially avoided by making void have
a unique instance.

--
Chris "electric hedgehog" Dollin
predicting self-predictors' predictions is predictably unpredictable.
Nov 15 '05 #5
Chris Dollin <ke**@hpl.hp.co m> wrote:
Robert Gamble wrote:
Chris Dollin wrote:
[1] No, I don't subscribe to the nonsense that `void` is an empty type.
6.2.5p19:
"The void type comprises an empty set of values; it is an incomplete
type that cannot be completed."


Yes, I know. That's why I said that I didn't subscribe to that
nonsense; because I don't. The view that `void` is an empty type
leads to unfortunate conclusions about the existance of functions
returning void, which are trivially avoided by making void have
a unique instance.


I think you have that backwards. If void had a unique instance, void
functions would return that instance, which could be used or assigned to
something. Since void is empty and does _not_ have any values, a void
function returns nothing, not even a unique instance, and you can't
assign nothing to an object.

Richard
Nov 15 '05 #6
Chris Dollin wrote:
Robert Gamble wrote:
Chris Dollin wrote:
maadhuu wrote:

> 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 is illegal because you are not permitted to dereference a void
pointer. That you clearly "intended" *p to be an int doesn't permit
the compiler to ignore the error.
In this case the printf statement is incorrect, but simply
dereferencing a valid void pointer is not illegal.
If *someVoidPointe r *were* legal


Which it is...


I beg to differ. It is not legal in standard C. Your very own quote
below says that `void` is an incomplete type, and incomplete types
cannot be dereferenced when I last looked.


void is an incomplete type, a pointer to void is not. We are talking
about dereferencing a pointer to void.
the result would be the unique
value of the void type [1], which isn't an int either.


It yields an expression of type void. Not sure what you mean by unique
value of void type.


Evaluating an expression of type T returns a result which is an
instance of T -- eg, `1+2` is an expression of type `int` who's
result is the value `3`, an instance of `int`.

The unique value of type `void` is the single, unique value which
is of type `void`.


That makes no sense.
[1] No, I don't subscribe to the nonsense that `void` is an empty type.


Oh, not one of those suckers who blindly subscribes to the Standard?


That's right.


Well there is little hope for you then. You talk about what is "legal
in standard C" and then admit that you don't accept the Standard itself
making your entire argument completely pointless.
6.2.5p19:
"The void type comprises an empty set of values; it is an incomplete
type that cannot be completed."


Yes, I know. That's why I said that I didn't subscribe to that
nonsense; because I don't.


If you think the Standard is nonsense then you have no ground to argue
what is or is not Standard complient.
The view that `void` is an empty type
leads to unfortunate conclusions about the existance of functions
returning void, which are trivially avoided by making void have
a unique instance.


You are going to have to elaborate on this.

Robert Gamble

Nov 15 '05 #7
printf is not part of the C language, its part of the library. The
compiler has no idea what %d in your string is supposed to mean. It
simply gets passed and interpretted at runtime. The C compiler doesn't
"look into" strings, nor what they are getting passed to.

Remember, printf could be overridden by your own code. You could
redefine printf to do something completely different and link it into
your code, which does not do anything at all with %d. printf is simply
a library function, not a part of the language itself.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 15 '05 #8
Jonathan Bartlett <jo*****@eskimo .com> wrote:

[ Do not post without content. Google are bad enough netizens to do so,
but this does not mean you should emulate them. ]
printf is not part of the C language, its part of the library.
That distinction is meaningless. printf() is part of a correct C
implementation.
The compiler has no idea what %d in your string is supposed to mean.
The compiler is _allowed_ to have no idea by the Standard. Passing
arguments to printf() that don't correspond with the format string
invokes undefined behaviour, which means that the implementation may or
may not notice, as it pleases. Had the Standard made this a constraint
violation, the implementation would have been required to diagnose this,
no matter which distinction between "compiler" and "library" many of
them currently make.
The C compiler doesn't "look into" strings,
This is misleading. For example, it certainly does look into them to
check for escape sequences. It is certainly also allowed to look into
printf()'s format string, and check the rest of its arguments; in fact,
some implementations do so.
Remember, printf could be overridden by your own code.
No, it couldn't. Again, _some_ implementations allow this; but in ISO C,
trying to override the Standard functions invokes undefined behaviour.
And in fact, it can have disastrous results if the rest of the library
happens to rely on that one function being the one they expect.
You could redefine printf to do something completely different and link
it into your code, which does not do anything at all with %d.
Not if you want it to run anywhere except on the implementation you
wrote it on, you can't.
printf is simply a library function, not a part of the language itself.


Again, in ISO C this distinction is to all intents and purposes devoid
of meaning. Either you have an ISO C implementation, or you do not.

Richard
Nov 15 '05 #9
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
[ Do not post without content. Google are bad enough netizens to do so,
but this does not mean you should emulate them. ]


ITYM "context"; only Skybuck posts without content :-)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #10

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

Similar topics

3
5691
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 know how to understand it, How to evaluate the expression as a void expression? explicit conversion the expression? but, befor the sentence ,"implicit or explicit conversions (except to void) shall not
9
5294
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 from integer of different size. Now I thought that when it was a void I could pass anything? Thing is it works when I use an int, but in this case I wanted to use a char. It wouldnt be hard to work around it, but it annoys me because I've heard...
9
3516
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
2703
by: Alex Vinokur | last post by:
Is this approach safe? class Foo { // Stuff }; void func1 (void *) { // Do something
4
2120
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 void ExternedSetDoFromCallback(void* (*DoGetFromCallback)()); // Client1.cpp
18
1813
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
2256
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 list_node *next; };
8
3626
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 says "is evaluated as a void expression, its value or designator is discarded." If I do the following: #include <stdio.h>
160
5658
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
8792
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
9266
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
9209
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...
0
8215
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...
1
6754
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
6054
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
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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.