NULL pointer and zero value

J

Jack Klein

Jack said:
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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

Jack Klein

Jack said:
ModuleFunction 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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
P

Peter Nilsson

stau said:
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.
 
P

pete

Jack said:
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 ?
 
P

Peter Pichler

pete said:
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.
 
P

Peter Pichler

pete said:
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.
 
P

pete

Peter said:
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.

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.
 
P

Peter Pichler

pete said:
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.
 
P

Peter Pichler

pete said:
Peter said:
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.
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
 
P

pete

Peter said:
pete said:
Peter said:
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.

garMF[0].func1, is a pointer, which is plain from the code
that you snipped, so how does "Only if" enter into it?
 
S

stau

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.

It's clear to all that Jack was generalizing.

struct some { int *a, *b, c } = {NULL}
Will work.

struct some2 { int a, *b, *c } = {NULL}
This may or may not work, depending on the definition of NULL (ie. if
it'just 0 (zero) it will work, if it's ((void *) 0) it'll not work.

So, I kind of agree that it's good idea to use 0 to initialize structs,
but of course, this was 1F/2*OT in this thread.
I personally like to use the explicit NULL when I'm using pointers.

Best regards,
Stau no Preto.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top