473,915 Members | 4,415 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 3488
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
5584
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
27238
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
4572
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
3701
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
4922
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
1986
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
24330
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
3386
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
2188
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
9991
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
10039
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...
1
11066
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
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9732
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...
0
5943
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4778
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
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3368
bsmnconsultancy
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...

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.