Some key points in the struct of structural body in C language _c language

Source: Internet
Author: User
Tags anonymous constant

I. Statement on the structure body
1, anonymous statement. Such as:

struct {
  int i,j;
} Point

Description
The meaning of this code is to declare an unnamed (anonymous) structure and create a struct variable point. If the declaration is placed in the global domain (outside of any function (such as the main function), the variable within the point will be initialized to the default value, in other words, when the struct variable is declared in this way, it has been allocated memory space.
Apply to the structure only need to produce a variable! In this case, the anonymous structure will have and only point this struct variable!
Different anonymous struct variables, types are different! Such as

struct {
  int i,j;
} P1,P2;

struct {
  int i,j;
} P3;

Ok if P1=P2 is to be p1=p3, the compiler prompts "incompatible types when assigning to type ' struct <anonymous> ' from type ' struct &L T;anonymous> ' ", the actual type of the two is not the same.

2. Explicitly declare a struct body

struct node{
  int i,j;

Declares a struct struct node, and if you need to declare one of its objects, you can do this: struct node n1;
You can declare multiple variables of the struct body.
Distinguishes between "struct variables in C" and "class objects in Java". C, "struct node N1;" Creates a struct variable and allocates memory space for it, not necessarily initialized! It depends on whether the variable is in the global domain; in Java, "Node N1;" Just declares a class object, that is, a "null reference", which can be imagined as a null pointer in C when "N1 = new Node ();" , N1 points to the memory space of the object. Therefore, in Java, it is possible to determine whether an object is empty by "N1==null", and in C, it cannot be judged by "n1==null" because "N1" is not a pointer, but rather the name of a type variable, like "int A;" This, obviously "a" is not a pointer!

3, using the typedef to simplify the structure of the formulation

typedefstruct {
  int i,j;
} Node;

The equivalent of renaming the code to node. Previously it was necessary to declare "struct node N1;" And now only "node N1."
In this code, if there is no TypeDef, the code means "declare an anonymous struct variable"! Notice the difference.
4. Declare the structural body variables in the structure body.

typedef struct {
  int i,j;
  Node N1;
} Node;

This piece of code is wrong!
Error 1: Declaring another struct directly in the struct, there will be a dead loop, such as a including b,b and including A,a and b ... So that the compiler can not know the space size of the structure, therefore, can not compile!
Error 2:typedef has not named the struct node, you have used node in the structure, and obviously the compiler doesn't know what node is at this point! So, can't compile!
The correct way to use this is as follows:

typedef struct node{
  int i,j;
  struct node *n1;
} Node;

Ii. the assignment of the structural body
1, declare a variable after the default value

typedef struct {
  char *p;
  int i;
  Char ch[256];
} mystr;
MyStr str;//declares a variable, and space has been allocated for it at this time!

As mentioned earlier, if this variable declaration is in the global, then "STR.P equals null,str.i equals 0,str.ch array is ' both '", the default initial value, if not global, all values are "wild values".

2, Manual initialization

MYSTR str2={"abc", 2, "def"};
MYSTR str3={.p= "abc",. ch= "Def"};
MyStr str4={.ch[256]= "def"};//error!
mystr str5={.ch[10]= "def"};//right!

At this point, the STR2 declaration is manually assigned an initial value. STR2.P and str2.ch are not the same when assigning values! STR2.P is a character pointer that points p to the address of the constant string "abc" in memory, while str2.ch is a constant character pointer (unable to manipulate the pointer), representing a character array, which means that the constant string "def" is literal to the CH array, and after the assignment ends, The value of CH is: ' d ', ' e ', ' f ', ' C ', ' C ' ...
It is also possible to initialize certain variables in the structure like STR3, notably STR4 and STR5. For arrays (such as Char a[size]), passed to the constant character pointer, can be "a", can be "a[n" (0<=n<size, the compiler ignores N), cannot be "a[size" (the compiler detects that the "array index in initializer exceeds array bounds ").

3. Assigning value

MyStr STR6;
STR6.P = "ABC";

Or

MyStr * pstr = & str6;//Gets the pointer to this structure variable
pstr->p = "abc";

4. Dynamic generation of structural body variables

MyStr * pstr = (mystr*) malloc (sizeof (MYSTR));
Pstr->p = "ABC";

Note that if the dynamically generated struct variable (malloc) is used, the memory space must be freed (with free) before the variable is discarded.
If a dynamically generated object exists within the structure, the internal memory space is released before releasing the structure, as follows

Pstr->p = (char*) malloc (sizeof (char) *256);
Free (pstr->p);
Free (PSTR);

Three, structure body array

We know that a variable array of basic data types can allocate space, and the struct can be considered as a new type, and it is also the way to define the variables that will automatically allocate space after the variable is declared.

struct book library[10];

This defines an array with 10 book variables, and the storage space is already allocated. The array of structures is the same as the normal array indexing method.

Structural body arrays can also use literal initialization methods, as follows

struct book lib[2] = {
 {"Apue", "Stevens", 128.0},
 {"CPP", "Prata", 60.0}
};

is not very convenient. Be aware that the outermost is {, not [OH.] For members that are structural variables of a struct, the same can be initialized with literal values, remembering that it is only initialized and cannot be used for assigning to struct variables.

Four, pointing to the structure of the pointer

The pointer to the structure is a point that has never been mastered, and I hope that it can be recorded well and strengthened.

For pointers there are several advantages, first: Just as a pointer to an array is easier to manipulate than the array itself, pointers to the structure are often easier to manipulate; second: In the early C, parameter passing can only use the structure of the pointer; Third: many wonderful data representations are made with a structure that contains pointers to other structures.

Unlike arrays, the name of a struct is not the address of the struct (that is, a separate structure name is not a synonym for that structure address), you must use the & operator. The way to declare a pointer is no different from a normal variable:

struct book *cpp;
struct Book C = {
 "c primer Plus",
 "Prata",
 60.1
};

CPP = &c;

Assuming that Lib is an array of struct book and now points to lib[0 with the structure pointer cpp, CPP+1 will point to lib[1 according to the operation rules of the pointer. Although in general understanding, the element in the structure is arranged in the memory, it can calculate the address difference between cpp+1 and CPP according to the size of each element. However, given the system's alignment requirements for storage, different systems may be aligned differently, so it is not appropriate to calculate the storage size of the structure by adding each member size together.

V. Members of the Access structure

This is simpler, noting that the structure is different from the way the pointer accesses the member of the mechanism, and the structure itself uses the. operator access, whereas pointers to the structure use-> access.

Strcut book CPP, *pcpp;
...
char *title;
title = Cpp.title;
title = pcpp->title;
title = (*pcpp). title; Because. Higher priority than *, must have parentheses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.