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

struct initialize

0 views
Skip to first unread message

June Lee

unread,
Mar 20, 2008, 4:12:03 AM3/20/08
to
what is it means by {0}, is that means initialize a struct to NULL?

ne_uri uri = {0};

typedef struct {
char *scheme;
char *host, *userinfo;
unsigned int port;
char *path, *query, *fragment;
} ne_uri;

gnuyuva

unread,
Mar 20, 2008, 4:26:00 AM3/20/08
to

It wont initialize the struct to NULL, but sets all struct elements
to
zero.

Victor Bazarov

unread,
Mar 20, 2008, 9:14:54 AM3/20/08
to

Which, BTW, means all the pointers are going to be null and 'port'
is going to be 0U.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


June Lee

unread,
Mar 20, 2008, 4:59:17 PM3/20/08
to
Can i do also??

ne_uri uri = {0,0,0,0};

>> typedef struct {
>> char *scheme;
>> char *host, *userinfo;
>> unsigned int port;
>> char *path, *query, *fragment;
>>
>> } ne_uri;

Default User

unread,
Mar 20, 2008, 5:46:41 PM3/20/08
to
June Lee wrote:

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>


Post rearranged to correct order.


> >> typedef struct {
> >> char *scheme;
> >> char *host, *userinfo;
> >> unsigned int port;
> >> char *path, *query, *fragment;
> > >
> >> } ne_uri;

> > Can i do also??
> ne_uri uri = {0,0,0,0};


Yes. This will explicitly set scheme, host, userinfo, and port to 0.
The remainder of the members will be zero-initialized. That doesn't
really buy you anything. A full listing might, for documentation
reasons, but the {0} form is idiomatic.

Brian

Andrey Tarasevich

unread,
Mar 20, 2008, 10:58:21 PM3/20/08
to
June Lee wrote:
> Can i do also??
>
> ne_uri uri = {0,0,0,0};
> ...

Yes. In C++ you can actually do just

ne_uri uri = {};

with the same effect.

--
Best regards,
Andrey Tarasevich

Andrey Tarasevich

unread,
Mar 20, 2008, 11:03:35 PM3/20/08
to
Default User wrote:
> A full listing might, for documentation
> reasons, but the {0} form is idiomatic.

It is mostly idiomatic in C. It is not as "idiomatic" in C++ since it will not
work when the first field cannot be initialized with explicit integral 0, but
still can be implicitly zero-initialized (field of enum type, for example). For
this reason, in C++ the corresponding idiomatic initializer is '{}'.

0 new messages