Home > Back-end >  Questions about structure initialized to zero
Questions about structure initialized to zero

Time:01-16

In network programming, we define structure, through memset or bzero to initialize it, the following
Struct sockaddr_in server;
Memset (& amp; Server, 0, sizeof (struct sockaddr_in));


Can be directly written
Struct sockaddr_in server {0}.

CodePudding user response:

You don't try to know

CodePudding user response:

Give you an example to know

 struct atest {
int a;
Int b=1;
};

Int main () {
Struct atest a {0};//this is initialized structure of the first member of
Struct atest b {0, 0};
Printf (" % d, % d \ n ", a.a, a.);//a. not initialized
Printf (" % d, % d \ n ", b.a, b.b);//b.b has been initialized, so each initialized to directly use memset to initialize faster than
}

CodePudding user response:

"Variable assignment" and "variable initialization" is not the same thing!
"Variable assignment" occur at runtime, grammar rules, their wording follow assignment
"Variable initialization" occur at compile time or run time, the writing follows initialization list syntax rules,
  • Related