Odd char string initialization in structs

G

gustavo

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 },
....
};
 
C

ciju

gustavo said:
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 },
...
};

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;
 
K

Kishore Yada

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
 
S

slebetman

gustavo said:
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 },
...
};

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}
};
 
A

ais523

gustavo said:
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 },
...
};
[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?
 
F

Flash Gordon

ais523 wrote:

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
 
E

edware

Flash said:
ais523 wrote:



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

Default User

ciju said:
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 },
...
};

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
 
F

Flash Gordon

edware wrote:

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
 
C

CBFalconer

Flash said:
edware wrote:



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/>
 
K

Keith Thompson

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

CBFalconer

Keith said:
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/>
 
R

Richard G. Riley

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

Robert Gamble

Richard said:
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
 
R

Richard G. Riley

Robert Gamble said:
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.
 

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

Similar Threads

packed structs 35
Problem with enums, const char* and structs 9
Copy string from 2D array to a 1D array in C 1
Copy array of structs in one go 24
structs 29
structs 12
packing and structs 5
copy initialization 3

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top