From: Robby on 8 Feb 2010 18:30 Hello, 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 so I could pass NULL as a function parameter... stuff like this: void f1(var1, NULL); ..... and so forth. I recently upgraded the compiler's version and now this still works but the compiler gives the follwoing warnings KERNEL.h:53:1: warning: "NULL" redefined In file included from C:/Program Files (x86)/Microchip/MPLAB C32 Suite/bin/../lib/gcc/pic32mx/3.4.4/../../../../pic32mx/include/GenericTypeDefs.h:58, from C:/Program Files (x86)/Microchip/MPLAB C32 Suite/bin/../lib/gcc/pic32mx/3.4.4/../../../../pic32mx/include/peripheral/i2c.h:50, from C:/Program Files (x86)/Microchip/MPLAB C32 Suite/bin/../lib/gcc/pic32mx/3.4.4/../../../../pic32mx/include/plib.h:46, C:/Program Files (x86)/Microchip/MPLAB C32 Suite/bin/../lib/gcc/pic32mx/3.4.4/../../../../pic32mx/include/stddef.h:67:1: warning: this is the location of the previous definition which I think means they have added a new definition of NULL in one of their files of their new compiler's version. So on the last warning above I double clicked on it an it took me to the following line of the stddef.h file: .... other code #ifndef __cplusplus #define NULL ((void *)0) // <<<<<<<<<<<<<< ....other code Now I know this question is probably better posted in the compiler's specific forum, but I would like to ask something about the language. What does this say exactly: #define NULL ((void *)0) NULL is a void pointer?? which still means 0 right? So if I use NULL anywhere, I am substituting it by 0 right? Why would someone do this as opposed to just do this: #define NULL 0 All feedback greatly appreciated! -- Best regards Roberto
From: Igor Tandetnik on 8 Feb 2010 19:49 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. > I recently upgraded the compiler's version and now this still works but the > compiler gives the follwoing warnings > > KERNEL.h:53:1: warning: "NULL" redefined > In file included from C:/Program Files (x86)/Microchip/MPLAB C32 > Suite/bin/../lib/gcc/pic32mx/3.4.4/../../../../pic32mx/include/GenericTypeDefs.h:58, Precisely my point. > Now I know this question is probably better posted in the compiler's > specific forum, but I would like to ask something about the language. What > does this say exactly: > > #define NULL ((void *)0) 0 cast to void* > NULL is a void pointer?? In C, typically, yes. > which still means 0 right? I'm not sure I understand this question. This is what C standard has to say: 6.3.2.3p3 An integer constant expression with the value 0, or such an expression cast to type void*, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function. 6.3.2.3p4 Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal. Footnote 55) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.17. > Why would someone do this as opposed to just do this: > > #define NULL 0 This is equally valid, and in fact this is how it's usually done in C++ ( ((void*)0) doesn't work in C++ because void* pointer is not implicitly convertible to other pointer types, the way it is in C). One possible reason to use ((void*)0) is that you would expect sizeof(NULL) to be equal to sizeof(void*) and not sizeof(int) (pointers don't have to be the same size as int). -- 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: Robby on 8 Feb 2010 21:20 "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. 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); But then, at first glance when I look at the latter function, I think that the function does some mathematical operation..... instead if I look at the former, I know that that parameter is used to carry out a certain mode of operation in the function. So what is best in these types of scenarios? > > I recently upgraded the compiler's version and now this still works but the > > compiler gives the follwoing warnings > > > > KERNEL.h:53:1: warning: "NULL" redefined > > In file included from C:/Program Files (x86)/Microchip/MPLAB C32 > > Suite/bin/../lib/gcc/pic32mx/3.4.4/../../../../pic32mx/include/GenericTypeDefs.h:58, > > Precisely my point. > > > Now I know this question is probably better posted in the compiler's > > specific forum, but I would like to ask something about the language. What > > does this say exactly: > > > > #define NULL ((void *)0) > > 0 cast to void* > > > 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. > This is what C standard has to say: > > 6.3.2.3p3 An integer constant expression with the value 0, or such an expression cast to type void*, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function. > 6.3.2.3p4 Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal. > Footnote 55) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.17. > > Why would someone do this as opposed to just do this: > > > > #define NULL 0 > > This is equally valid, and in fact this is how it's usually done in C++ ( ((void*)0) > doesn't work in C++ because void* pointer is not implicitly convertible to other pointer types, the way it is in C). One possible reason to use ((void*)0) is that you would expect sizeof(NULL) to be equal to sizeof(void*) and not sizeof(int) (pointers don't have to be the same size as int). Thanks Igor.
From: Pavel A. on 8 Feb 2010 21:43 "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 ); --pa
From: Igor Tandetnik on 9 Feb 2010 00:11 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". > 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. -- 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
|
Next
|
Last
Pages: 1 2 3 4 Prev: Books on Shell and gadgets programming Next: StartupInfo in CreateProcess for ShowWindow |