469,592 Members | 1,713 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Odd char string initialization in structs

I was looking at the Sendmail's source code, and i've got confused
about this kind of initialization:

------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
....
};
------------------------

I'm familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain?

Apr 27 '06 #1
14 2166

gustavo wrote:
I was looking at the Sendmail's source code, and i've got confused
about this kind of initialization:

------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
...
};
------------------------

I'm familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain?


It is just the initialization of an array of structure.
It can be done in another way in your code.

Eg:
PrivayValues[0].field1 = "public";
PrivayValues[0].field2 = PRIV_PUBLIC;

PrivayValues[1].field1 = "needmailhelo";
PrivayValues[1].field2 = PRIV_NEEDMAILHELO;

-----
PrivayValues[n].field1 = "--";
PrivayValues[n].field2 = --;

Apr 27 '06 #2
can you also provide what is struct prival..

I suppose it must be

struct prival
{
char *x;
int y;
};
In the above case,
one element of the struct can be created using

struct prival OneElement = { "anystring", ANY_INTEGER };

The first member might be confusing. What it does is
1. Stores "anystring" into the string table.
2. Puts a pointer in OneElement.x

In a C program whereever string constants occur, they are replaced by
pointers and the string constants are moved to the string table.

But, array initialization is an exception to that.

when we write string[] = "foo",
the compiler actually takes it as

string[4] = { 'f', 'o', 'o' , '\0' };

and therefore "foo" does not go to the string table at all.
Regards,
Yada Kishore

Apr 27 '06 #3
gustavo wrote:
I was looking at the Sendmail's source code, and i've got confused
about this kind of initialization:

------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
...
};
------------------------

I'm familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain?


char string[] = "foo";

is actually just a convenient shortcut of the more general form:

char string[] = {
'f' ,
'o' ,
'o' ,
'\0'
};

The general form applies to all types in C, for example:

int example[] = {
100,
200,
300
};

this includes structs, for example:

struct thing {
char *name;
int size;
};

struct thing stuff[] = {
{"Me", 100},
{"Myself", 200},
{"I", 300}
};

Apr 27 '06 #4
gustavo wrote:
I was looking at the Sendmail's source code, and i've got confused
about this kind of initialization:

------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
...
};
------------------------

I'm familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain?

[The quote above is to identify the thread]

After reading the responses to this thread, I started wondering whether
the 'universal initialiser' {0} could apply to types like int which can
be initialised normally. So I wrote the program

#include <stdio.h>

int i = {0};
int j = {1};

int main(void)
{
printf("%d %d\n",i,j);
return 0;
}

which compiles on c89-conforming gcc with warnings turned up to the max
with no diagnostics, printing
0 1
as expected. Is it really possible to initialise anything like this?

Apr 27 '06 #5
ais523 wrote:

<snip>
After reading the responses to this thread, I started wondering whether
the 'universal initialiser' {0} could apply to types like int which can
be initialised normally. So I wrote the program

#include <stdio.h>

int i = {0};
int j = {1};

int main(void)
{
printf("%d %d\n",i,j);
return 0;
}

which compiles on c89-conforming gcc with warnings turned up to the max
with no diagnostics, printing
0 1
as expected. Is it really possible to initialise anything like this?


Yes, this is perfectly legal C and is defined as doing what you expect.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Apr 27 '06 #6
Flash Gordon wrote:
ais523 wrote:

<snip>
After reading the responses to this thread, I started wondering whether
the 'universal initialiser' {0} could apply to types like int which can
be initialised normally. So I wrote the program

#include <stdio.h>

int i = {0};
int j = {1};

int main(void)
{
printf("%d %d\n",i,j);
return 0;
}

which compiles on c89-conforming gcc with warnings turned up to the max
with no diagnostics, printing
0 1
as expected. Is it really possible to initialise anything like this?


Yes, this is perfectly legal C and is defined as doing what you expect.


struct test_t {
char *a;
int b;
double c;
};

int main(void)
{
struct test_t test = { 0 };
}

Will this initialization set the whole struct to 0,
or just the first member, byte, or something like that?
Apr 27 '06 #7
ciju wrote:

gustavo wrote:
I was looking at the Sendmail's source code, and i've got confused
about this kind of initialization:

------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
...
};
------------------------

I'm familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain?


It is just the initialization of an array of structure.
It can be done in another way in your code.

Eg:
PrivayValues[0].field1 = "public";
PrivayValues[0].field2 = PRIV_PUBLIC;


This is not the same thing at all. You have assignment, not
initialization. If field1 happens to be an array of char rather than a
pointer to char, your version would be illegal.

Similarly, if either field1 or field2 happened to be const.


Brian
Apr 27 '06 #8
edware wrote:

<snip>
struct test_t {
char *a;
int b;
double c;
};

int main(void)
{
struct test_t test = { 0 };
}

Will this initialization set the whole struct to 0,
or just the first member, byte, or something like that?


It will initialise the entire struct so appropriate 0 type values (null
pointers, 0, 0.0 as appropriate). If you initialise any of it to any
value then the rest gets initialised to an appropriate 0 value for the type.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Apr 27 '06 #9
Flash Gordon wrote:
edware wrote:

<snip>
struct test_t {
char *a;
int b;
double c;
};

int main(void)
{
struct test_t test = { 0 };
}

Will this initialization set the whole struct to 0,
or just the first member, byte, or something like that?


It will initialise the entire struct so appropriate 0 type values
(null pointers, 0, 0.0 as appropriate). If you initialise any of
it to any value then the rest gets initialised to an appropriate
0 value for the type.


NOT to null pointers. Those you have to handle yourself.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Apr 27 '06 #10
CBFalconer <cb********@yahoo.com> writes:
Flash Gordon wrote:
edware wrote:

<snip>
struct test_t {
char *a;
int b;
double c;
};

int main(void)
{
struct test_t test = { 0 };
}

Will this initialization set the whole struct to 0,
or just the first member, byte, or something like that?


It will initialise the entire struct so appropriate 0 type values
(null pointers, 0, 0.0 as appropriate). If you initialise any of
it to any value then the rest gets initialised to an appropriate
0 value for the type.


NOT to null pointers. Those you have to handle yourself.


YES to null pointers.

C99 6.7.8p10:

If an object that has automatic storage duration is not
initialized explicitly, its value is indeterminate. If an object
that has static storage duration is not initialized explicitly,
then:

-- if it has pointer type, it is initialized to a null pointer;

-- if it has arithmetic type, it is initialized to (positive or
unsigned) zero;

-- if it is an aggregate, every member is initialized
(recursively) according to these rules;

-- if it is a union, the first named member is initialized
(recursively) according to these rules.

C99 6.7.8p21:

If there are fewer initializers in a brace-enclosed list than
there are elements or members of an aggregate, or fewer characters
in a string literal used to initialize an array of known size than
there are elements in the array, the remainder of the aggregate
shall be initialized implicitly the same as objects that have
static storage duration.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 28 '06 #11
Keith Thompson wrote:
CBFalconer <cb********@yahoo.com> writes:
Flash Gordon wrote:
edware wrote:
<snip>

struct test_t {
char *a;
int b;
double c;
};

int main(void)
{
struct test_t test = { 0 };
}

Will this initialization set the whole struct to 0,
or just the first member, byte, or something like that?

It will initialise the entire struct so appropriate 0 type values
(null pointers, 0, 0.0 as appropriate). If you initialise any of
it to any value then the rest gets initialised to an appropriate
0 value for the type.


NOT to null pointers. Those you have to handle yourself.


YES to null pointers.

C99 6.7.8p10:

If an object that has automatic storage duration is not
initialized explicitly, its value is indeterminate. If an object
that has static storage duration is not initialized explicitly,
then:

-- if it has pointer type, it is initialized to a null pointer;

-- if it has arithmetic type, it is initialized to (positive or
unsigned) zero;

-- if it is an aggregate, every member is initialized
(recursively) according to these rules;

-- if it is a union, the first named member is initialized
(recursively) according to these rules.

C99 6.7.8p21:

If there are fewer initializers in a brace-enclosed list than
there are elements or members of an aggregate, or fewer characters
in a string literal used to initialize an array of known size than
there are elements in the array, the remainder of the aggregate
shall be initialized implicitly the same as objects that have
static storage duration.


Thanks for the correction. I was confusing it with the
'all-bits-zero' case, and in fact I did know better, because I
frequently use that characteristic to control auto-initialization.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 29 '06 #12
CBFalconer <cb********@yahoo.com> writes:
It will initialise the entire struct so appropriate 0 type values
(null pointers, 0, 0.0 as appropriate). If you initialise any of
it to any value then the rest gets initialised to an appropriate
0 value for the type.


NOT to null pointers. Those you have to handle yourself.


Then my ISO setting on gnu c doesnt work : because it certainly does
here. Come to think of it, it would be ludicrous if it didn't.
May 2 '06 #13
Richard G. Riley wrote:
CBFalconer <cb********@yahoo.com> writes:
It will initialise the entire struct so appropriate 0 type values
(null pointers, 0, 0.0 as appropriate). If you initialise any of
it to any value then the rest gets initialised to an appropriate
0 value for the type.


NOT to null pointers. Those you have to handle yourself.


Then my ISO setting on gnu c doesnt work : because it certainly does
here. Come to think of it, it would be ludicrous if it didn't.


Right, Chuck acknowleged this a couple of days ago.
However, even if the pointers were not guaranteed to be initialized it
wouldn't mean that a comforming implementation couldn't do so or that
they might not wind up as NULL for some other reason.

Robert Gamble

May 2 '06 #14
"Robert Gamble" <rg*******@gmail.com> writes:
Richard G. Riley wrote:
CBFalconer <cb********@yahoo.com> writes:
>> It will initialise the entire struct so appropriate 0 type values
>> (null pointers, 0, 0.0 as appropriate). If you initialise any of
>> it to any value then the rest gets initialised to an appropriate
>> 0 value for the type.
>
> NOT to null pointers. Those you have to handle yourself.
>


Then my ISO setting on gnu c doesnt work : because it certainly does
here. Come to think of it, it would be ludicrous if it didn't.


Right, Chuck acknowleged this a couple of days ago.
However, even if the pointers were not guaranteed to be initialized it
wouldn't mean that a comforming implementation couldn't do so or that
they might not wind up as NULL for some other reason.

Robert Gamble


So he (begrudgingly) did. I apologise for repeating this.
May 2 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Petter Reinholdtsen | last post: by
33 posts views Thread by Jordan Tiona | last post: by
5 posts views Thread by wkaras | last post: by
15 posts views Thread by thinktwice | last post: by
14 posts views Thread by mdh | last post: by
24 posts views Thread by DomoChan | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.