From: Ulrich Eckhardt on 9 Feb 2010 03:57 Robby wrote: > "Igor Tandetnik" wrote: >> Robby wrote: >>> NULL is a void pointer?? >> >> In C, typically, yes. >> >>> which still means 0 right? >> >> I'm not sure I understand this question. > > Isn't a void pointer's value 0 ? Or should I ask, what is the difference > between a void pointer's value and the value of 0. A void pointer doesn't have any type, 'void' is rather a placeholder, but it does have a value. This value can be null or but it doesn't have to. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Igor Tandetnik on 9 Feb 2010 07:36 Ulrich Eckhardt wrote: > A void pointer doesn't have any type This is not true formally, and I don't think it's useful to think this way informally either: 6.2.5p19 The void type comprises an empty set of values; it is an incomplete type that cannot be completed. The type of a void pointer is, of course, void*. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Barry Schwarz on 9 Feb 2010 08:04 On Tue, 9 Feb 2010 00:11:45 -0500, "Igor Tandetnik" <itandetnik(a)mvps.org> wrote: >Robby wrote: >> "Igor Tandetnik" wrote: >> >>> Robby wrote: >>>> As you all know, I used to use a non C compliant compiler. Now in the new C >>>> compiler, I declared a NULL in a header file like this: >>>> >>>> #define NULL 0 >>> >>> Do you have to? NULL should be defined in standard headers that come with any decent C compiler. >> >> hum! Well, what if I do this for my own null: >> >> #define null 0 >> >> I do this because sometimes I have a function's parameter that takes an >> integer. > >I wouldn't if I were you. You would not use the word "apple" to describe an orange: it makes equally little sense to use the word "null" to >describe something other than a null pointer. > >> Now, in this function, if this integer is 1 it will do one thing, if >> its 2, it will do another and if its 3, it will do even another, but if its >> 0, it will reset some stuff and do none of the the other things. > >As Pavel notes, you want an enum. > >>>> which still means 0 right? >>> >>> I'm not sure I understand this question. >> >> Isn't a void pointer's value 0 ? > >I'm not sure what you mean by "the value of a pointer". Given ptr, a pointer to any object type, the expression ptr == 0 is well defined by the language. It will evaluate to true whenever ptr is a null pointer. However, whether a pointer is a null pointer depends on its current value, not its type. Given the sequence void *ptr; ptr = &ptr; the expression ptr == 0 is guaranteed to evaluate to false. But this use of the == operator is a special case in the language. The fact that a pointer can be compared to 0 in no way implies the representation of the pointer matches that of an int. Even if the two objects have the same size, after int i = 0; void *p = 0; i = memcmp(&i, &p, sizeof i); you still know nothing about the value of i. Given both of the above, its fair to say the value of a void pointer, or any other pointer for that matter, is not 0 in the general case. It is guaranteed to compare to 0 in the specific case using the == operator if the pointer has been assigned the null pointer constant value. > >> Or should I ask, what is the difference >> between a void pointer's value and the value of 0. If the pointer points to an object, the value is guaranteed to be different from 0. > >The former is of type void*, the latter is of type int. I wonder if the OP stops at null or maybe goes on to define bool as float or true as 0. The opportunities to write even more confusing code are almost boundless. -- Remove del for email
From: Robby on 9 Feb 2010 11:42 "Pavel A." wrote: > "Robby" <Robby(a)discussions.microsoft.com> wrote in message > news:3D18A66B-759E-4ED2-85FF-6D980E65C6B4(a)microsoft.com... > > > hum! Well, what if I do this for my own null: > > > > #define null 0 > > > > I do this because sometimes I have a function's parameter that takes an > > integer. Now, in this function, if this integer is 1 it will do one thing, > > if > > its 2, it will do another and if its 3, it will do even another, but if > > its > > 0, it will reset some stuff and do none of the the other things. So > > sometimes > > its possible that I would need that integer to be passed in as 0, so I > > call > > the function like this: > > > > void f1(NULL); > > > > well now it will be: > > > > void f1(null); > > > > or I could simply do: > > > > void f1(0); > > .................................. > > By convention (or even by standard?), NULL is null pointer, not numeric 0. > For what you want, there are enums: > > enum xxxx { > reset_some stuff = 0, > do_one_thing= 1, > ........ etc......... > } > > f( reset_some stuff ); Yes I do this too in my code, but now I feel confirmed that enum is the right chioce that should be used for this. I use enum alot and I also use null as a 0 parameter, I think I will stop doing that. Thanks Pavel. Rob
From: Robby on 9 Feb 2010 12:20 "Igor Tandetnik" wrote: > > I do this because sometimes I have a function's parameter that takes an > > integer. > > I wouldn't if I were you. You would not use the word "apple" to describe an orange: it makes equally little sense to use the word "null" to describe something other than a null pointer. > > > Now, in this function, if this integer is 1 it will do one thing, if > > its 2, it will do another and if its 3, it will do even another, but if its > > 0, it will reset some stuff and do none of the the other things. > > As Pavel notes, you want an enum. Yes, understood, that won't be hard to modify my code. > >>> which still means 0 right? > >> > >> I'm not sure I understand this question. > > > > Isn't a void pointer's value 0 ? > > I'm not sure what you mean by "the value of a pointer". > > > Or should I ask, what is the difference > > between a void pointer's value and the value of 0. > > The former is of type void*, the latter is of type int. Looks like I am not the only guy that's confused about this, see: http://www.lysator.liu.se/c/c-faq/c-1.html Cheeezzze guys, this could get confusing. I can live with the following simple explanations as quoted from: http://www.geekinterview.com/talk/1016-void-pointer-in-c-programming-language.html [" Void is a non-type which has no size, no format, it is a black hole from which you cannot read. void * is pointer-to-void. A void pointer is a pointer which can point to any data type (which of course requires typecasting). Where as a null pointer is used to indicate that it is pointing nowhere. A null pointer is a value that any pointer may take to represent that it is pointing to "nowhere", while a void pointer is a special type of pointer that can point to somewhere without a specific type. One refers to the value stored in the pointer itself and the other to the type of data it points to. "] But, where is this "nowhere" ???? "Between you and I, everything points to something, if it points to a location conataining the value of 0, then lets say it. Regards Robert
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Books on Shell and gadgets programming Next: StartupInfo in CreateProcess for ShowWindow |