468,503 Members | 2,126 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,503 developers. It's quick & easy.

array initialization

Hello,

I was wondering if the following statement will initialize all the 6
elements with NULL:

void function() {
int *list[6] = {NULL};
. ..
}

what I want is an array with 6 pointers to int that are initialized
with NULL. Or do I've to do

int *list[6] = {NULL, NULL, NULL, NULL, NULL, NULL} ?

thanks for your help,
+kind regards,

Arne

Aug 24 '06 #1
15 3255
ar*********@gmail.com wrote:
>
Hello,

I was wondering if the following statement will initialize all the 6
elements with NULL:

void function() {
int *list[6] = {NULL};
. ..
}
Yes, it will.
A short initializer for an array,
means that all of the elements
following the initialized one or initialized ones,
are initialized with a value of zero,
and zero means the same thing as NULL
when used to initialize a pointer.
what I want is an array with 6 pointers to int that are initialized
with NULL. Or do I've to do

int *list[6] = {NULL, NULL, NULL, NULL, NULL, NULL} ?
No, you don't.

--
pete
Aug 24 '06 #2
int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.

Thu, 24 Aug 2006 15:49:10 -0700´╝îarne.muller wrote:
Hello,

I was wondering if the following statement will initialize all the 6
elements with NULL:

void function() {
int *list[6] = {NULL};
. ..
}

what I want is an array with 6 pointers to int that are initialized
with NULL. Or do I've to do

int *list[6] = {NULL, NULL, NULL, NULL, NULL, NULL} ?

thanks for your help,
+kind regards,

Arne
Aug 25 '06 #3
assiss wrote:
int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.
I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 25 '06 #4
Eric Sosman wrote:
assiss wrote:
int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.

I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.
assiss may be confusing the *value* of a null pointer constant with the
*representation* of a null pointer constant. He would be well-advised
to re-read Chapter 5 of the FAQ <http://c-faq.com/>.

Robert Gamble

Aug 25 '06 #5
Robert Gamble wrote:
Eric Sosman wrote:
>assiss wrote:
>>int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.
I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.

assiss may be confusing the *value* of a null pointer constant with the
*representation* of a null pointer constant. He would be well-advised
to re-read Chapter 5 of the FAQ <http://c-faq.com/>.

Robert Gamble
thanks. I do forget someting about NULL.
Aug 25 '06 #6
Eric Sosman wrote:
assiss wrote:
>int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.

I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.
Oh? Consider:

#include <stdio.h>

int main(void);
int n = 0;

if (n == NULL) puts('You are right');
else puts('You are wrong');
return 0;
}

Note that I did NOT write "if (0 == NULL)"

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!
Aug 25 '06 #7
Thanks for your replies! From¨ what I read in the FAQ one can use 0 or
NULL (macro) - but the little prog below just gives me a segmentation
fault ;-(

kind regards,

Arne

CBFalconer wrote:
Eric Sosman wrote:
assiss wrote:
int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.
I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.

Oh? Consider:

#include <stdio.h>

int main(void);
int n = 0;

if (n == NULL) puts('You are right');
else puts('You are wrong');
return 0;
}

Note that I did NOT write "if (0 == NULL)"

--
Chuck F (cb********@yahoo.com) (cb********@maineline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.netUSE maineline address!
Aug 25 '06 #8
CBFalconer wrote:
Eric Sosman wrote:
>>assiss wrote:

>>>int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.

I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.


Oh? Consider:

#include <stdio.h>

int main(void);
int n = 0;

if (n == NULL) puts('You are right');
else puts('You are wrong');
return 0;
}

Note that I did NOT write "if (0 == NULL)"
... and I for my part did NOT write "NULL equals zero."

(Also, it looks like your double-quote key is functioning
erratically: Sometimes it omits the left or right half -- hard
to tell which -- of its symbol. Have it checked before it gets
even worse, and maybe catches fire. ;-)

Back to the thread: assiss, Chuck and I have been having a
little fun at your expense, and I apologize. Here's the scoop:
Every variable that's "initialized in part" is "initialized in
full." If you provide an initializer for just a few elements
of an array or struct, all the other elements are initialized
for you. The elements you don't initialize explicitly are all
initialized to "zeroes of the proper type:" 0.0 for double,
'\0' for char, (char*)0 for char* pointers, and so on. (Union
objects are a little different because they can hold only one
value at a time, so when a union is initialized this way what
happens is that its first element is given a zero of the type
appropriate to that element.) This all works even on machines
where the various kinds of zero and NULL might not be represented
as all-bits-zero; it's the compiler's business to make it work.

What variables are initialized this way? Two kinds: those
with static storage duration (variables outside functions plus
`static' variables inside functions), and any variables for
which you provide a partial initializer:

int a; /* a == 0 */
int b = 3; /* b == 3 */
int c[4] = {1,2}; /* c == 1,2,0,0 */
struct s {
int x, y;
} d = { 1 }; /* d.x == 1, d.y == 0 */
union u {
char *cp;
int i;
float f;
} e; /* e.cp == (char*)0 */

void f(void) {
static int fa; /* fa == 0 */
int fc[4] = {9}; /* fc == 9,0,0,0 */
struct s fd = { 2 }; /* fd.x == 2, fd.y == 0 */
int v; /* NOT INITIALIZED */
}

The newer "C99" version of the Standard allows some fancier
forms of initializers (for example, you can provide explicit
initializers for elements [5],[1],[9] of an array, in that order),
but even then the rule holds: Initialize any part of something,
and all the parts you don't initialize receive zeroes.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 25 '06 #9
Eric Sosman wrote:
CBFalconer wrote:
>Eric Sosman wrote:
>>assiss wrote:
int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.

I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.


Oh? Consider:

#include <stdio.h>

int main(void);
int n = 0;

if (n == NULL) puts('You are right');
else puts('You are wrong');
return 0;
}

Note that I did NOT write "if (0 == NULL)"

... and I for my part did NOT write "NULL equals zero."

(Also, it looks like your double-quote key is functioning
erratically: Sometimes it omits the left or right half -- hard
to tell which -- of its symbol. Have it checked before it gets
even worse, and maybe catches fire. ;-)

Back to the thread: assiss, Chuck and I have been having a
little fun at your expense, and I apologize. Here's the scoop:
Every variable that's "initialized in part" is "initialized in
full." If you provide an initializer for just a few elements
of an array or struct, all the other elements are initialized
for you. The elements you don't initialize explicitly are all
initialized to "zeroes of the proper type:" 0.0 for double,
'\0' for char, (char*)0 for char* pointers, and so on. (Union
objects are a little different because they can hold only one
value at a time, so when a union is initialized this way what
happens is that its first element is given a zero of the type
appropriate to that element.) This all works even on machines
where the various kinds of zero and NULL might not be represented
as all-bits-zero; it's the compiler's business to make it work.

What variables are initialized this way? Two kinds: those
with static storage duration (variables outside functions plus
`static' variables inside functions), and any variables for
which you provide a partial initializer:

int a; /* a == 0 */
int b = 3; /* b == 3 */
int c[4] = {1,2}; /* c == 1,2,0,0 */
struct s {
int x, y;
} d = { 1 }; /* d.x == 1, d.y == 0 */
union u {
char *cp;
int i;
float f;
} e; /* e.cp == (char*)0 */

void f(void) {
static int fa; /* fa == 0 */
int fc[4] = {9}; /* fc == 9,0,0,0 */
struct s fd = { 2 }; /* fd.x == 2, fd.y == 0 */
int v; /* NOT INITIALIZED */
}

The newer "C99" version of the Standard allows some fancier
forms of initializers (for example, you can provide explicit
initializers for elements [5],[1],[9] of an array, in that order),
but even then the rule holds: Initialize any part of something,
and all the parts you don't initialize receive zeroes.
Thanks very much.
I check out ansi_c99 and find that I have some wrong understandings
about initializations.
Aug 25 '06 #10
ar*********@gmail.com wrote:
>
Thanks for your replies! From¨ what I read in the FAQ one can use
0 or NULL (macro) - but the little prog below just gives me a
segmentation fault ;-(
Please do not top-post. Some details and reasons in the links
below.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Aug 25 '06 #11
Eric Sosman posted:
union u {
char *cp;
int i;
float f;
} e; /* e.cp == (char*)0 */

Are you saying that zero-intialisation for a union-type results in zero-
initialisation of its first member?

--

Frederick Gotham
Aug 25 '06 #12
Frederick Gotham wrote:
Eric Sosman posted:
union u {
char *cp;
int i;
float f;
} e; /* e.cp == (char*)0 */


Are you saying that zero-intialisation for a union-type results in zero-
initialisation of its first member?
I think he is saying that a static variable of union type has it's
first member initialized to zero which is correct.

Robert Gamble

Aug 25 '06 #13


Frederick Gotham wrote On 08/25/06 10:16,:
Eric Sosman posted:

> union u {
char *cp;
int i;
float f;
} e; /* e.cp == (char*)0 */

Are you saying that zero-intialisation for a union-type results in zero-
initialisation of its first member?
Give the man a cigar! (Section 6.7.8, paragraph 10.)

Notes: (1) The example above came from a larger example
in which it was clear that the declaration was at file scope,
hence had static storage duration, and hence was initialized
even without an explicit initializer. (2) It's actually not
the union's first element that gets initialized, but its
first *named* element.

union u2 {
int : 4; /* unnamed bit-field */
int : 13; /* another one */
char *cp; /* first named element */
double d;
} f; /* f.cp == (char*)0 */

Unnamed elements in unions seem pretty useless, but as far
as I can see they're permitted.

--
Er*********@sun.com

Aug 25 '06 #14
assiss wrote:

Your reply belongs under (or interspersed with) the text you are
replying to. NEVER above. I've corrected it this time.
Thu, 24 Aug 2006 15:49:10 -0700´╝îarne.muller wrote:
>Hello,

I was wondering if the following statement will initialize all the 6
elements with NULL:

void function() {
int *list[6] = {NULL};
. ..
}
<snip>
int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.
Wrong. The C standard defines that the other five elements will be
initialised to null pointers (not NULL which is a macro name). This will
be the case however null pointers are represented.
--
Flash Gordon.
Aug 25 '06 #15
On Fri, 25 Aug 2006 03:03:11 UTC, Eric Sosman
<es*****@acm-dot-org.invalidwrote:
assiss wrote:
int *list[6] = {NULL};
list[0]=NULL, then the other 5 elements will be initialized with 0.

ONLY if you are sure that NULL equals to 0.
On any conforming C compiler NULL will like 0 or '\0' or 0x00 or 000
simply expand to a null pointer constant in that context. That is NOT
0.
>
I am sure that NULL equals 0. Always. On every
Standard-conforming C implementation ever built. Amen.
No, noways. It will always expand to an null pointer constant. There
is a difference.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 25 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fred Zwarts | last post: by
13 posts views Thread by simondex | last post: by
19 posts views Thread by Henry | last post: by
8 posts views Thread by Peter B. Steiger | last post: by
15 posts views Thread by Charles Sullivan | last post: by
3 posts views Thread by kk_oop | last post: by
5 posts views Thread by toton | last post: by
15 posts views Thread by jamx | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.