473,799 Members | 3,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 },
{ "needmailhe lo", PRIV_NEEDMAILHE LO },
{ "needexpnhe lo", PRIV_NEEDEXPNHE LO },
{ "needvrfyhe lo", PRIV_NEEDVRFYHE LO },
....
};
------------------------

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
14 2419
CBFalconer <cb********@yah oo.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_Keit h) 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********@yah oo.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.c om, 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/googlegroupsrep ly/>
Apr 29 '06 #12
CBFalconer <cb********@yah oo.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********@yah oo.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*******@gmai l.com> writes:
Richard G. Riley wrote:
CBFalconer <cb********@yah oo.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4159
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even tried prefixing them with BASE:: but to no avail. Still it's ok when I set them in the construtor body, not the init list. Does this mean that it's a small advantage of the initialization inside the constructor over initialization in the init...
20
7101
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the string literal.
0
1358
by: Kurt Ng | last post by:
Need help quick!!! Am really stuck on this problem! I have a C dll, and it uses a nested struct. (see below) The struct has 2 layers of nested structs. The first field in the first struct is a char*. I used StructLayout.Sequential to port each of the structs to C#, and I used UnmanagedType.LPStr to marshal the char* to C# string, but the resulting struct data are stored wrong when passed into the dll's api call. The char* is...
33
3680
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God has prepared for those who love him" 1 Cor 2:9
10
5361
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is it a fair statement that char *ptr will take 4 more bytes (on 32bit platform) in DATA segment? I have found
5
2394
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
15
26438
by: thinktwice | last post by:
char a = { 0 } is it ok?
14
71142
by: mdh | last post by:
And I thought I understood it, finally. Alas. given: char *s={"Jan","Feb","Mar","April"}; is it possible to have char *p point at s? *p = s...does not do it, as I expect. *p=s...I believe sets it to point to the first element in s. But
24
2198
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is this really valid c++ or c? where can I find out how this is implemented? Im concerned because I had a 3rd party library wrapper which was
0
10491
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...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
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
9079
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
7571
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
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
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...
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.