Common use methods for C-language structures (structs)

Source: Internet
Author: User

This article turns from CSDN huqinweI987

Basic definition: structure, popular speaking is like packaging packaging, some common characteristics (such as the property belonging to a certain kind of thing, often a business-related attribute aggregation) of variables encapsulated inside, through a certain method access to modify internal variables.

structure Definition:

The first type : only the struct definition

[CPP] View Plaincopy
    1. struct stuff{
    2. Char job[20];
    3. int age;
    4. float height;
    5. };

The second type : the struct-body definition of the initialization of the struct variable attached to the struct type

[CPP] View Plaincopy
    1. Directly with variable name Huqinwei
    2. struct stuff{
    3. Char job[20];
    4. int age;
    5. float height;
    6. }huqinwei;

Perhaps the initial look is not accustomed to easily confused, in fact, this is equivalent to:

[CPP]View Plaincopy
    1. struct stuff{
    2. Char job[20];
    3. int age;
    4. float height;
    5. };
    6. struct Stuff Huqinwei;

The Third Kind: if the struct you use only one variable Huqinwei, and no longer need to use

[CPP]View Plaincopy
    1. struct Stuff yourname;
To define a second variable.

Then, the structure definition of the additional variable initialization can further simplify the Third Kind :

[CPP]View Plaincopy
    1. struct{
    2. Char job[20];
    3. int age;
    4. float height;
    5. }huqinwei;
It is more concise to remove the struct name, but it is not possible to define other identical structural variables-at least I do not have this method at the moment.

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

Around the mouth, huh? The concept of struct variables and the internal member variables of the struct is to be distinguished.

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

[CPP]View Plaincopy
    1. struct Stuff yourname;
The definition of its member variable can be made with the declaration:

[CPP]View Plaincopy
    1. struct stuff Huqinwei = {"manager", 30,185};

You can also consider the assignment between structs:

[CPP]View Plaincopy
    1. struct Stuff faker = Huqinwei;
    2. or struct stuff faker2;
    3. Faker2 = Faker;
    4. Print, each member variable of the visible structure is identical

If you do not use the top two methods, then the operation of the member array is slightly cumbersome (with a for loop may be better)

[CPP]View Plaincopy
    1. Huqinwei.job[0] = ' M ';
    2. HUQINWEI.JOB[1] = ' a ';
    3. Huqinwei.age = 27;
    4. nbsp Huqinwei.height = 185;

Struct member variables can be accessed in addition to the symbol ".", which is also accessible with "--".

Pointers and Arrays:

This is the topic that can never be opened, the first is quoted:

[CPP]View Plaincopy
    1. struct Stuff *ref = &Huqinwei;
    2. Ref->age = 100;
    3. printf ("Age is:%d\n", huqinwei.age);

The pointers are the same.

[CPP] View Plaincopy
    1. struct Stuff *ptr;
    2. Ptr->age = 200;
    3. printf ("Age is:%d\n", huqinwei.age);

Structs also cannot be mundane and must have arrays:

[CPP]View Plaincopy
  1. struct test{
  2. int a[3];
  3. int b;
  4. };
  5. For the simultaneous existence of arrays and variables, there are the following definition methods:
  6. struct Test Student[3] = {{66,77,55},0},
  7. {{44,65,33},0},
  8. {{46,99,77},0}};
  9. In particular, it can be simplified into:
  10. struct Test Student[3] = {{66,77,55,0},
  11. {44,65,33,0},
  12. {46,99,77,0}};

Variable length structural body

An array that can be variable in length

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. typedef struct changeable{
  5. int icnt;
  6. Char pc[0];
  7. }schangeable;
  8. Main () {
  9. printf ("size of struct changeable:%d\n",sizeof (schangeable));
  10. Schangeable *pchangeable = (schangeable *) malloc (sizeof (schangeable) + 10*sizeof (char));
  11. printf ("Size of pchangeable:%d\n",sizeof (pchangeable));
  12. Schangeable *pchangeable2 = (schangeable *) malloc (sizeof (schangeable) + 20*sizeof (char));
  13. pchangeable2->icnt = 20;
  14. printf ("pchangeable2->icnt:%d\n", pchangeable2->icnt);
  15. strncpy (pchangeable2->pc,"Hello World", 11);
  16. printf ("%s\n", pchangeable2->pc);
  17. printf ("Size of Pchangeable2:%d\n",sizeof (pchangeable2));
  18. }

Run results

[CPP]View Plaincopy
    1. Size of struct Changeable:4
    2. Size of Pchangeable:4
    3. Pchangeable2->icnt:20
    4. Hello World
    5. Size of Pchangeable2:4

The length of the struct itself is an int length (this int is usually only meant to represent the length of the array behind), and the length of the array behind is not counted, but the array can be used directly.

(Is that a pointer behind it?) The pointer also occupies the length! This is not accounted for! The principle is very simple, this thing is completely behind the tail of the array, malloc opened up a continuous space. In fact, this should not be a mechanism, the feeling should be more like a skill bar )

 

Structure nesting:

Structure nesting In fact there is no too unexpected things, as long as the following certain rules:

[CPP]View Plaincopy
    1. For "affair", only the final structure of the variable interest, where a, B can also be deleted, but preferably with
    2. struct a{
    3. struct b{
    4. int C;
    5. }
    6. b
    7. }
    8. A
    9. Use the following methods to access:
    10. A.B.C = 10;
In particular, you can define structure B on one side and use it on the other:

[CPP]View Plaincopy
    1. struct a{
    2. struct b{
    3. int C;
    4. }b;
    5. struct B sb;
    6. }a;
How to use and test:

[CPP]View Plaincopy
    1. A.B.C = 11;
    2. printf ("%d\n", A.B.C);
    3. A.SB.C = 22;
    4. printf ("%d\n", A.SB.C);
    5. The result is unmistakable.

struct and function:

For the reference, first:

[CPP]View Plaincopy
    1. void func (int);
    2. Func (A.B.C);
Using an INT member variable in a struct as something like a normal int variable is a way to think without thinking.

The other two are the transfer of copies and pointers:

[CPP]View Plaincopy
  1. struct a definition ibid.
  2. Two functions are established to pass the struct a struct and its pointers, respectively.
  3. void Func1 (struct a a) {
  4. printf ("%d\n", A.B.C);
  5. }
  6. void Func2 (struct * A) {
  7. printf ("%d\n", A->B.C);
  8. }
  9. Main () {
  10. A.B.C = 112;
  11. struct A * PA;
  12. PA = &a;
  13. Func1 (a);
  14. Func2 (&a);
  15. FUNC2 (PA);
  16. }   

Occupy Memory Space:

struct struct, cannot request memory space when the struct is defined, but if it is struct variable , it can be allocated when declaring--the relationship is like C + + class and object, the object allocates memory (but strictly speaking, as a code snippet, The structure Definition section ". Text" really does not occupy space? Of course, this is another category of topic).

The size of the struct is the sum of the variable size contained in the struct, and cannot be used as a "char a[]" (flexible) variable, which must be clearly sized, and the size of the above structure is printed below:

  [CPP]View Plaincopy
    1. printf ("size of struct man:%d\n",sizeof (struct man));
    2. printf ("size:%d\n",sizeof (Huqinwei));
    3. The result is no suspense, all 28: char array,int variable 4, floating-point variable 4.

Unlike C + + classes, structs cannot initialize internal variables of a struct.

As below, for the error demonstration:

[CPP]View Plaincopy
    1. #include <stdio.h>
    2. Directly with variable name Huqinwei
    3. struct stuff{
    4. Char job[20] = "Programmer";
    5. Char job[];
    6. int age = 27;
    7. float height = 185;
    8. }huqinwei;

PS: The declaration of the struct should also pay attention to the location, the scope is not the same.

C + + is slightly different from the declaration definition of struct variables, which is more "object-oriented" style and less demanding.

So familiar with the common methods, we should pay attention to what often make mistakes, see C language Structure common errors.

Click to view original: http://blog.csdn.net/huqinwei987/article/details/23625823

C language Structure (struct) common use method (GO)

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.