473,699 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3470
ar*********@gma il.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.mul ler 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********@yah oo.com) (cb********@mai neline.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********@yah oo.com) (cb********@mai neline.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 "initialize d in part" is "initialize d 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 "initialize d in part" is "initialize d 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5568
by: Fred Zwarts | last post by:
If I am right, members of a class that are const and not static must be initialized in the initialization part of a constructor. E.g. class C { private: const int I; public: C(); };
13
27104
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
19
4548
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
8
3678
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
15
4882
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with memset(). E.g., static struct mystruct_st { int x1;
3
1978
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value initialization when the type is instantiated. Any suggestions for a mod that would allow array initialization syntax? ***********Here's the type: #ifndef CHECKED_ARRAY_ #define CHECKED_ARRAY_
5
24309
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a default ctor). Is it possible to do so in the class parameter initialization using specific ctor?
15
3370
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
2
2177
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
152
9833
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
0
8704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
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,...
0
9192
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8940
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,...
0
7781
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6546
isladogs
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...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
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
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.