array initialization

A

arne.muller

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
 
P

pete

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

assiss

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

Eric Sosman

assiss said:
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.
 
R

Robert Gamble

Eric said:
assiss said:
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
 
A

assiss

Robert said:
Eric said:
assiss said:
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.
 
C

CBFalconer

Eric said:
assiss said:
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)"
 
A

arne.muller

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
Eric said:
assiss said:
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)"
 
E

Eric Sosman

CBFalconer said:
Eric said:
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.
 
A

assiss

Eric said:
CBFalconer said:
Eric said:
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.
 
F

Frederick Gotham

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?
 
R

Robert Gamble

Frederick said:
Eric Sosman posted:



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
 
E

Eric Sosman

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





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

Flash Gordon

assiss wrote:

Your reply belongs under (or interspersed with) the text you are
replying to. NEVER above. I've corrected it this time.
Hello,

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

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

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

Herbert Rosenau

assiss said:
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!
 

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
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top