473,698 Members | 2,158 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 3327
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
5687
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
5289
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
3514
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
2698
by: Alex Vinokur | last post by:
Is this approach safe? class Foo { // Stuff }; void func1 (void *) { // Do something
4
2115
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
1806
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
2246
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
3624
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
5635
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
9170
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, 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...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7739
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
5862
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
4371
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.