struct initialize

J

June Lee

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

gnuyuva

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;

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

Victor Bazarov

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

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

V
 
D

Default User

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.

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
 
A

Andrey Tarasevich

Default said:
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 '{}'.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top