If I have a pointer char * p, is it correct to assign NULL to this
pointer by:
"memset( &p, 0, sizeof(p));" instead of "p = NULL;"
The reason I ask is I have an array of structure of N function
variables, for example,
typedef struct
{
int (* func1)();
int (* func2)();
void * (* func2)(int );
} ModuleFunctions ;
#define N 100
ModuleFunction garMF[N];
When initializing, instead of making a loop to assgin NULL to each
function member of struct ModuleFunction for each member of array, is
it correct to do something like
memset( garMF, 0, sizeof(ModuleFu nctions)*N );
Thanks for your help,
DT
Nov 14 '05
31 3539
On Wed, 31 Dec 2003 12:46:37 GMT, pete <pf*****@mindsp ring.com> wrote
in comp.lang.c: Jack Klein wrote: On Tue, 30 Dec 2003 14:07:21 GMT, pete <pf*****@mindsp ring.com wrote in comp.lang.c:
> > typedef struct > > { > > int (* func1)(); > > int (* func2)(); > > void * (* func2)(int ); > > } ModuleFunctions ; > > > > #define N 100 > > ModuleFunction garMF[N]; ModuleFunction garMF[N] = {NULL};
I would much prefer {0} to {NULL} here.
All of the members of all of the elements of the array will be initialized to NULL. Why do you like {0} better ?
I should have elaborated. {0} works in all cases to initialize any
static or automatic scalar or aggregate type.
The C standard allows two definitions for the macro NULL. For
simplicity's sake, they are "0" and "(void *)0", or equivalent. My
experience is that most (but definitely not all) C compilers use the
cast to (void *) rather than the raw numeric constant.
In this case, since the first element of the structure has pointer
type, either definition of NULL will work. If the structure had a
non-pointer first member, it would be correct on implementations that
used a raw 0 for NULL, but be a constraint violation on those that
defined NULL as "(void *)0".
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
On Tue, 30 Dec 2003 22:01:29 GMT, Kevin Goodsell
<us************ *********@never box.com> wrote in comp.lang.c: Jack Klein wrote:
On Tue, 30 Dec 2003 14:07:21 GMT, pete <pf*****@mindsp ring.com> wrote in comp.lang.c:
ModuleFunctio n garMF[N] = {NULL};
I would much prefer {0} to {NULL} here.
Is {NULL} even correct here if NULL happens to be #defined with a cast to void*?
I should have elaborated. {0} works in all cases to initialize any
static or automatic scalar or aggregate type.
The C standard allows two definitions for the macro NULL. For
simplicity's sake, they are "0" and "(void *)0", or equivalent. My
experience is that most (but definitely not all) C compilers use the
cast to (void *) rather than the raw numeric constant.
In this case, since the first element of the structure has pointer
type, either definition of NULL will work. If the structure had a
non-pointer first member, it would be correct on implementations that
used a raw 0 for NULL, but be a constraint violation on those that
defined NULL as "(void *)0".
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
On Wed, 31 Dec 2003 15:37:42 +1100, Peter Nilsson wrote: I think Jack prefers {0} because it's a more consistent (and well understood) paradigm in general.
No, he explained why,and it makes logic.
"stau" <st**@pretogal. pt> wrote in message
news:pa******** *************** *****@pretogal. pt... On Wed, 31 Dec 2003 15:37:42 +1100, Peter Nilsson wrote:
I think Jack prefers {0} because it's a more consistent (and well understood) paradigm in general.
No, he explained why,and it makes logic.
How is "{0} works in all cases..." significantly different to what I said?
It's more succinct and better phrased, I grant you, but I don't see how my
sentence runs contrary to it.
P.S. Jack's replies were not available on my news server at the time I made
the post.
--
Peter
Jack Klein wrote: On Wed, 31 Dec 2003 12:46:37 GMT, pete <pf*****@mindsp ring.com> wrote in comp.lang.c:
Jack Klein wrote: On Tue, 30 Dec 2003 14:07:21 GMT, pete <pf*****@mindsp ring.com wrote in comp.lang.c:
> > > typedef struct > > > { > > > int (* func1)(); > > > int (* func2)(); > > > void * (* func2)(int ); > > > } ModuleFunctions ; > > > > > > #define N 100 > > > ModuleFunction garMF[N];
> ModuleFunction garMF[N] = {NULL};
I would much prefer {0} to {NULL} here.
All of the members of all of the elements of the array will be initialized to NULL. Why do you like {0} better ?
I should have elaborated. {0} works in all cases to initialize any static or automatic scalar or aggregate type.
What you said, could also be used to explain a preference for
char *pointer = 0;
over
char *pointer = NULL;
Is that your preference also ?
--
pete
"pete" <pf*****@mindsp ring.com> wrote... Jack Klein wrote: I should have elaborated. {0} works in all cases to initialize any static or automatic scalar or aggregate type. What you said, could also be used to explain a preference for char *pointer = 0; over char *pointer = NULL;
Not quite. Since a void* is implicitly converted to any type*, the two
examples are exactly equivalent regardless of any of the two standard
definitions of NULL.
Is that your preference also ?
My preference is the latter, but it is only a style issue, nothing else.
Peter Pichler wrote: "pete" <pf*****@mindsp ring.com> wrote... Jack Klein wrote: I should have elaborated. {0} works in all cases to initialize any static or automatic scalar or aggregate type.
What you said, could also be used to explain a preference for char *pointer = 0; over char *pointer = NULL;
Not quite. Since a void* is implicitly converted to any type*, the two examples are exactly equivalent regardless of any of the two standard definitions of NULL.
Exactly quite.
ModuleFunction garMF[N] = {NULL};
and
ModuleFunction garMF[N] = {0};
are exactly equivalent regardless of any of the two standard
definitions of NULL. Is that your preference also ?
My preference is the latter, but it is only a style issue, nothing else.
Style issues are on topic.
Reasons for prefering one style over another
which involve legibility and maintainability ,
are the more interesting ones.
--
pete
"pete" <pf*****@mindsp ring.com> wrote... Jack Klein wrote: I should have elaborated. {0} works in all cases to initialize any static or automatic scalar or aggregate type. What you said, could also be used to explain a preference for char *pointer = 0; over char *pointer = NULL;
Not quite. Since a void* is implicitly converted to any type*, the two
examples are exactly equivalent regardless of any of the two standard
definitions of NULL.
Is that your preference also ?
My preference is the latter, but it is only a style issue, nothing else.
"pete" <pf*****@mindsp ring.com> wrote... Jack Klein wrote: I should have elaborated. {0} works in all cases to initialize any static or automatic scalar or aggregate type. What you said, could also be used to explain a preference for char *pointer = 0; over char *pointer = NULL;
Not quite. Since a void* is implicitly converted to any type*, the two
examples are exactly equivalent regardless of any of the two standard
definitions of NULL.
Is that your preference also ?
My preference is the latter, but it is only a style issue, nothing else.
"pete" <pf*****@mindsp ring.com> wrote... Peter Pichler wrote: "pete" <pf*****@mindsp ring.com> wrote... Jack Klein wrote:
What you said, could also be used to explain a preference for char *pointer = 0; over char *pointer = NULL;
Not quite. Since a void* is implicitly converted to any type*, the two examples are exactly equivalent regardless of any of the two standard definitions of NULL.
Exactly quite. ModuleFunction garMF[N] = {NULL}; and ModuleFunction garMF[N] = {0}; are exactly equivalent regardless of any of the two standard definitions of NULL.
Only if garMF[N] is a pointer.
If I remember correctly, the thread started with explicit initialization
of struct members with memset, {NULL} or {0}. Of those three, memset can
only be portably used if the struct is a wrapper for an usigned char array,
anything else may lead to a UB. {NULL} can only be used if the first member
of such struct is a pointer. {0}, however, can be used in any context. Is that your preference also ?
My preference is the latter, but it is only a style issue, nothing else.
Style issues are on topic. Reasons for prefering one style over another which involve legibility and maintainability , are the more interesting ones.
I didn't say it was not on-topic. Whether or not it is a /good/ topic
is quite a different question though ;-)
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: junky_fellow |
last post by:
Can 0x0 be a valid virtual address in the address space
of an application ?
If it is valid, then the location pointed by a NULL pointer
is also valid and application should not receive "SIGSEGV"
( i am talking of unix machine ) while trying to read that
location.
Then how can i distinguish between a NULL pointer and an invalid
location ?
Is this essential that NULL pointer should not point to any of
the location in the virtual address...
|
by: Jason Curl |
last post by:
I've been reading this newsgroup for some time and now I am thoroughly
confused over what NULL means.
I've read a NULL pointer is zero (or zero typecast as a void pointer),
others say it's compiler dependent (and that NULL might be anything, but
it is always NULL).
The source snippet is below. The question is:
- When I use calloc to allocate a block of memory, preinitialising it to
zero, is this equivalent (and portable C) to...
|
by: yossi.kreinin |
last post by:
Hi!
There is a system where 0x0 is a valid address, but 0xffffffff isn't.
How can null pointers be treated by a compiler (besides the typical
"solution" of still using 0x0 for "null")?
- AFAIK C allows "null pointers" to be represented differently then
"all bits 0". Is this correct?
- AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work
just like `void* p=NULL'. Is this correct?
|
by: fieldfallow |
last post by:
Hello all,
Before stating my question, I should mention that I'm fairly new to C.
Now, I attempted a small demo that prints out the values of C's numeric
types, both uninitialised and after assigning them their maximum defined
values. However, the output of printf() for the long double 'ld' and the
pointer of type void 'v_p', after initialisation don't seem to be right.
The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
|
by: Quantum |
last post by:
Hi,
Are these equivalent:
char text;
if(text==NULL){}
if(text==0){}
Thank you,
| |
by: lovecreatesbea... |
last post by:
Do you prefer malloc or calloc?
p = malloc(size);
Which of the following two is right to get same storage same as the
above call?
p = calloc(1, size);
p = calloc(size, 1);
|
by: khan |
last post by:
Hi,
I read that pointer representation can non-zero bit pattern, machine
specific.Compiler when comes accross value '0' in pointer context,
converts it to machine specific null pointer bit-pattern.
My question is if a program refers to that specific value which is
used by the machine to refer to null pointer, how compiler treats
that?.
|
by: prashant.khade1623 |
last post by:
I am not getting the exact idea.
Can you please explain me with an example.
Thanks
|
by: copx |
last post by:
I don't know what to think of the following..
(from the dietlibc FAQ)
Q: I see lots of uninitialized variables, like "static int foo;". What
gives?
A: "static" global variables are initialized to 0. ANSI C guarantees that.
Technically speaking, static variables go into the .bss ELF segment,
while "static int foo=0" goes into .data. Because .bss is zero
filled by the OS, it does not need to be in the actual binary. So it
is in fact...
|
by: rahul |
last post by:
#include <stdio.h>
int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}
This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.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 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,...
| |
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |