Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initializing Structure members to null

2,555 views
Skip to first unread message

Luis D. Larin Jr.

unread,
Mar 30, 1997, 3:00:00 AM3/30/97
to

Someone please help me.
Is there a way to initialize char Structure member variables to null or
maybe to 0.
Below is the structure declaration.
struct record {

char empnum[11];
char emprest[51];

};
struct record emp[50];
struct record temp;


How can I initialize all of the member variables to null?

I've tried a loop like below what am i missing?

void initrecord(void)
{
int i;
char tempc[11] = {"\0"};

for (i=0; i < 50; i++)
{

emp[i].empnum = tempc;

}

}

I will be very grateful for any help.

llarin
lla...@mailhost.tcs.tulane.edu

Message has been deleted

Lawrence Kirby

unread,
Mar 31, 1997, 3:00:00 AM3/31/97
to

In article <01bc3ccd$9f29a4c0$240e...@llarin.tcs.tulane.edu>

lla...@mailhost.tcs.tulane.edu "Luis D. Larin Jr." writes:

>Someone please help me.
>Is there a way to initialize char Structure member variables to null or
>maybe to 0.
>Below is the structure declaration.
>struct record {
>
> char empnum[11];
> char emprest[51];
>
>};
>struct record emp[50];
>struct record temp;
>
>
>How can I initialize all of the member variables to null?

You can use an initialiser, for example:

>struct record emp[50] = { 0 };
>struct record temp = { 0 };

The simple rule with initialisers is that if you initialise anything in
an object then then whole object is initialised. Fields that you don't
provide an explicit initialiser for are initialised as if they were assigned
the value 0 (so floating point fields get set to 0.0 and pointers get set to
null).

>I've tried a loop like below what am i missing?
>
>void initrecord(void)
>{
> int i;
> char tempc[11] = {"\0"};

This is another example where the the whole array get initialised to zero
values.

>
> for (i=0; i < 50; i++)
> {
>
> emp[i].empnum = tempc;

This doesn't work because emp[i]/empnum is an array and you can't assign
to arrays (even though you can initialise them in definitions). In this
case since the array elemns have an integral type you can write:

memset(emp[i].empnum, 0, sizeof emp[i].empnum);

Note that memset isn't a portable way of initialising floating point or
pointer objects.

> }

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Alicia Carla Longstreet

unread,
Mar 31, 1997, 3:00:00 AM3/31/97
to Luis D. Larin Jr.

Luis D. Larin Jr. wrote:

> Someone please help me.
> Is there a way to initialize char Structure member variables to null or
> maybe to 0.
> Below is the structure declaration.
> struct record {
> char empnum[11];
> char emprest[51];
> };
> struct record emp[50];
> struct record temp;

> How can I initialize all of the member variables to null?

> I've tried a loop like below what am i missing?

> void initrecord(void)
> {
> int i;
> char tempc[11] = {"\0"}; /* forget this, it is unnecessary. */

>
> for (i=0; i < 50; i++)
> {
>
> emp[i].empnum = tempc; /* You have to use strcpy() to coppy strings. */

What you want to do right here is:

emp[i].empnum[0] = '\0';
emp[i].emprest[0] = '\0';

> }

> }

The above will properly initialize every element in your array to NULL
strings.

--
********************************************
* Alicia Carla Longstreet ca...@ici.net
********************************************
The money spent on an abortion,
as any woman may find out,
can better be spent on other things,
like taking the kids to an amusment park, or on vacation.

Al Bowers

unread,
Apr 3, 1997, 3:00:00 AM4/3/97
to

On Mon, 31 Mar 1997 09:25:01 -0500, Alicia Carla Longstreet
<ca...@ici.net> wrote:
>Luis D. Larin Jr. wrote:
>> Someone please help me.
>> Is there a way to initialize char Structure member variables to null or
>> maybe to 0.
>> Below is the structure declaration.
>> struct record {
>> char empnum[11];
>> char emprest[51];
>> };
>> struct record emp[50];
>
>What you want to do right here is:
>
> emp[i].empnum[0] = '\0';
> emp[i].emprest[0] = '\0';

struct record emp[50] = {0}; should be all
that is needed.

If there are fewer initializers than the total
number of elements in an array, then the trailing
members are initialized with 0.
So, given
int array[10] = {0};
Each and every element of the int array will
be initialized at 0.

The same goes for a structure.
Given
struct record {
int empnum;
double empsalary;
};
struct record jones = {0};
Both members of the structure (empnum and empsalary)
will initialize at 0.


An aggregate is a structure or array. If an aggregate contains
members of aggregate type, the initialization rules apply
recursively.
Given;
struct record {
int empnum;
double empsalary;
int empaccts[10];
char empname[30];
};
struct record employee[50] = {0}; should be all
that is needed.

This will initialize each member of the structure
and each member of the subaggregate to 0.
For example, employee[10].empacct[5] will be initialized
at 0 just as will employe[5].empname[5].


Al Bowers
Tampa, FL
mailto:abo...@combase.com
http://www.gate.net/~abowers/index.html

Coen Engelbarts

unread,
Apr 3, 1997, 3:00:00 AM4/3/97
to

Coen Engelbarts wrote:

>
> Alicia Carla Longstreet wrote:
> > Luis D. Larin Jr. wrote:
> > > Someone please help me.
> > > Is there a way to initialize char Structure member variables to null or
> > > maybe to 0.
> > What you want to do right here is:
> >
> > emp[i].empnum[0] = '\0';
> > emp[i].emprest[0] = '\0';
>
> I recently discovered that K&R2 section A8.7, page 219, reads:
> "If there are fewer initializers in the list than members of the
> structure, the trailing members are initialized with 0."
>
> That means, that you can NOT leave out the initialization of emprest[0].
> Or is '\0' not guaranteed to be 0?

Aaaarghh! What was I thinking?
The K&R2 quote is about initialization at the declaration of the struct,
with an 'init-declarator'.
What Luis and Alicia wrote, is an assignment.
That means, that you can certainly NOT leave out the initialization of emprest[0]
in this case.

However, you can initialize the structures at declaration, like this:
struct record emp[50] = { "", "" };
where "" is an empty string, i.e. a '\0' char.

I'm not sure if '\0' is guaranteed to be 0 (K&R2 page 193 does not help either),
but I suspect that
struct record emp[50] = { "" };
and/or
struct record emp[50] = { '\0' };
would also work (well, at least on most implementations).
Does anyone know that for sure?
--
Coen Engelbarts E-mail: coen.en...@cmg.nl
CMG Telecommunications & Utilities B.V.
Division Advanced Technology Tel: +31 30 23 39 300
P.O.Box 8038 Fax: +31 30 23 39 495
3503 RA Utrecht, The Netherlands

Coen Engelbarts

unread,
Apr 3, 1997, 3:00:00 AM4/3/97
to

Alicia Carla Longstreet wrote:
> Luis D. Larin Jr. wrote:
> > Someone please help me.
> > Is there a way to initialize char Structure member variables to null or
> > maybe to 0.
> What you want to do right here is:
>
> emp[i].empnum[0] = '\0';
> emp[i].emprest[0] = '\0';

I recently discovered that K&R2 section A8.7, page 219, reads:
"If there are fewer initializers in the list than members of the
structure, the trailing members are initialized with 0."

That means, that you can leave out the initialization of emprest[0].


Or is '\0' not guaranteed to be 0?

Daniel P Hudson

unread,
Apr 3, 1997, 3:00:00 AM4/3/97
to

Luis D. Larin Jr. wrote:

> Someone please help me.
> Is there a way to initialize char Structure member variables to null or
> maybe to 0.

> Below is the structure declaration.
> struct record {
> char empnum[11];
> char emprest[51];
> };
> struct record emp[50];

> struct record temp;

> How can I initialize all of the member variables to null?

Well, I'll just use temp as an example.

struct record temp = {{'\0'}, {'\0'}};
both members of the object temp will be initilized to ""

other methods include declaring them static, learn what this
means before doing it though [although it won't likely have
serious side effects], looping through each element of the object
array, etc...

Lawrence Kirby

unread,
Apr 5, 1997, 3:00:00 AM4/5/97
to

In article <334383...@cmg.nl>
coen.en...@cmg.nl "Coen Engelbarts" writes:

>However, you can initialize the structures at declaration, like this:
> struct record emp[50] = { "", "" };
>where "" is an empty string, i.e. a '\0' char.

I believe you can initialise *any* object definition with = { 0 }. All
integer fields within the object get initialised to 0, all floating point
fields get initialised to 0.0 (converted to the appropriate type) and all
pointers get initialised to null.

>I'm not sure if '\0' is guaranteed to be 0 (K&R2 page 193 does not help either),
>but I suspect that

'\0' is an int constant with value 0. So barring preprocessor tricks it
behaves exactly the same as 0. All-bits-zero is guaranteed to represent an
integer value of zero, but there may be other representations of zero, e.g.
all-bits-one on a 1's complement archetecture. Even on a 2's complement
archetecture there may be holes in the representation of an int.

> struct record emp[50] = { "" };
>and/or
> struct record emp[50] = { '\0' };
>would also work (well, at least on most implementations).
>Does anyone know that for sure?

This will work on all conforming implementations. The 2nd initialiser is the
same as = { 0 };

Al Bowers

unread,
Apr 6, 1997, 4:00:00 AM4/6/97
to

On Sat, 05 Apr 97 22:04:51 GMT, fr...@genesis.demon.co.uk (Lawrence
Kirby) wrote:
>> struct record emp[50] = { "" };
>>and/or
>> struct record emp[50] = { '\0' };
>>would also work (well, at least on most implementations).
>>Does anyone know that for sure?
>
>This will work on all conforming implementations. The 2nd initialiser is the
>same as = { 0 };

Using struct record emp[50] = {""}; might work in this scenerio where
the first member is a character array. However, should the
structure's first member be an int, for example, this initialization
will probably generate computer warnings about incompatible
types. The first member, an int, may or may not be zero.

The safe way is to initialize {0}. The first member and
all remaining members are initialized to 0.

0 new messages