C language summary -- structure (struct) Usage

Source: Internet
Author: User

Today, I will review struct and explore some small details that I didn't pay attention to before:


Basic definition: struct. In general, it is like packaging and encapsulation. It encapsulates variables with common features (for example, attributes of the same class of things) inside, use some methods to access and modify internal variables.



Struct definition:


First: Only struct is defined.

struct stuff{        char job[20];        int age;        float height;};


Second: Struct definition for variable Initialization

// Directly include the variable name Huqinweistruct stuff {char job [20]; int age; float height;} Huqinwei;

It may be difficult to get confused at the early stage. In fact, this is equivalent:

struct stuff{        char job[20];        int age;        float height;};struct stuff Huqinwei;

Third:If the struct uses only one variable Huqinwei

struct stuff yourname;
Define the second variable.

Then, the struct definition for variable initialization can be further simplified.Third:

struct{        char job[20];        int age;        float height;}Huqinwei;
Remove the struct name, which is more concise, but cannot define other variables with the same struct-at least I do not know this method now.

Definition and access of struct variables and their internal member variables:

Detour? The concepts of struct variables and member variables must be distinguished.


As mentioned in the second type, the declaration of struct variables can be used:

struct stuff yourname;
The member variables can be defined with the declaration:

   struct stuff Huqinwei = {"manager",30,185};

You can also consider the assignment between struct values:

Struct stuff faker = Huqinwei; // or struct stuff faker2; // faker2 = faker; print. Each member variable of the struct is exactly the same.

If you do not use the above two methods, the operations on the member array will be a little troublesome (it may be better to use the for loop)

        Huqinwei.job[0] = 'M';        Huqinwei.job[1] = 'a';        Huqinwei.age = 27;        Huqinwei.height = 185;

In addition to the symbol ".", you can also use "->" to access struct member variables (which will be mentioned below ).



Pointers and arrays:

This is a topic that will never be around. The first is reference:


Struct stuff * ref = & Huqinwei; ref-> age = 100; printf ("age is: % d \ n", Huqinwei. age); print visible changes

The same is true for pointers.

        struct stuff *ptr;        ptr->age = 200;        printf("age is:%d\n",Huqinwei.age);200 


Struct cannot be vulgar. arrays must exist:


Struct test {int a [3]; int B ;}; // If arrays and variables exist at the same time, as defined below: struct test student [3] ={{{ 66,77, 55}, 0 },{ {44,65, 33}, 0 },{ {46,99, 77}, 0 }}; // In particular, it can be simplified to: struct test student [3] = }};


Nested struct:

Struct Nesting is nothing unexpected, as long as it follows a certain rule:

// For "A hammer sale", where A and B can be deleted, but it is best to bring struct a {struct B {int c ;} B;}; // use the following method for access:. b. c = 10;
Specifically, you can define the struct 'B' while using it:

struct A{        struct B{                int c;        }b;        struct B sb;}a;
Usage and testing:

A. b. c = 11; printf ("% d \ n",. b. c);. sb. c = 22; printf ("% d \ n",. sb. c); the result is correct.


Struct and function:

For parameter passing, first:

void func(int);func(a.b.c);
Using the int member variable in the struct as the same thing as a common int variable is a method that you don't need to think about.

The other two are the transfer of copies and pointers:

// Struct A is defined as above. // two functions are set up to pass the struct A struct and Its pointer respectively. Void func1 (struct A) {printf ("% d \ n",. b. c);} void func2 (struct A * a) {printf ("% d \ n", a-> B. c);} main () {. b. c = 112; struct A * pa; pa = & a; func1 (a); func2 (& a); func2 (pa );}



Memory usage:

Struct, inStructMemory space cannot be applied for during definition, but if it isStruct variableAnd can be allocated during declaration. The relationship between the two is like the class and object of C ++, and the object is allocated memory (but strictly speaking, as a code segment, the struct definition section ". is text really not occupying space ?).


The size of the struct is the sum of the variable sizes contained in the struct, and the flexible variable "char a []" cannot be used. The size must be specified. The size of the preceding struct is printed below:

Printf ("size of struct man: % d \ n", sizeof (struct man); printf ("size: % d \ n", sizeof (Huqinwei )); there is no suspense in the results. They are all 28: char array 20, int variable 4, and floating point variable 4.

Unlike C ++ classes, struct cannot beStruct internal variablesInitialization ,.

For example:

# Include
 
  
// Directly include the variable name Huqinweistruct stuff {// char job [20] = "Programmer"; // char job []; // int age = 27; // float height = 185;} Huqinwei;
 



Related Article

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.