C structure initialization and assignment

https://www.cnblogs.com/luoxiao23/p/11230412.html

1. Initialization structure

Structure is a common type of custom configuration, is a very common method of data packing. Initializes the object structure a number of ways, into specified initialization, the initialization sequence, the constructor initializes. If the following structure.

struct A
{
    int b;
    int c; }

(1) Specify initialization (Designated Initializer) There are two ways to achieve on a dot by addition of assignments to achieve, i.e., ".fieldname = value", another is achieved by a colon, i.e. "fieldname: value", where fieldname is the name given to the structure members. The former is a structure initialized C99 standard introduced, but in C ++, many compilers do not support.

//点号+赋值符号
struct A a={.b = 1,.c = 2}; //冒号 struct A a={b:1,c:2};

When initialized with like Linux kernel ".fieldname = value" manner, using the specified initialization, a significant advantage is variable and the number of members of the initialization sequence, and scalability, such as increasing field, to avoid the traditional initialization sequence to bring the heavily modified.

Initialization (2) the order is our most popular initialized, because it is more simple to write, but relative to the specified initialization, initialization sequence can not be changed, poor flexibility.

struct A a1={1,2};

(3) The constructor to initialize common in C ++ code, because C ++ struct can be seen in the class, the structure can also have constructors, so we can initialize the object structure by the constructor structure. A given structure with a constructor:

struct A 
{
    A(int a,int b) { this->a=a; this->b=b; }; int b; int c; }

Then initialize the object structure can initialize the class object as above, the following form:

struct A a(1,2);

Note: struct if the definition of a constructor, it can not be used to initialize the braces that can not be initialized with the specified sequence initialized.

2. The structure assignment

Assignment and initialization of variables is not the same, the initialization is done when the variable definition, be part of the variable definition, assignments is to change the operation when the value of the variable taken after the completion of variable definitions. Given the structure or A:

struct A
{
    int b;
    int c; }

Note: the assignment of the variable structure is not employed braces manner assignment, for example the following assignment is disallowed.

struct A  a;
//错误赋值 a={1,2};

The following method common structure variable assignment list.

(1) Use of structure variables memset blanking operation:

//按照编译器默认的方式进行初始化(如果a是全局静态存储区的变量,默认初始化为0,如果是栈上的局部变量,默认初始化为随机值)
struct A a; memset(&a,0,sizeof(a));

(2) turn to each structure member variable assignment:

struct A a; 
a.b=1; a.c=2;

(3) using the existing structure to another structure of the variable variable. That is between the structure variable can be assigned to each other.

struct A a={1,2}; struct A aa; aa=a; //将已有的结构体变量付给aa

Initialization and assignment essentially different, initialization is first assignment when the variable definition, assignment is different on the change operation, the concept of value after definition, so the realization is not the same.

Guess you like

Origin www.cnblogs.com/focus-z/p/11742310.html