Summary of C structure body

Source: Internet
Author: User

Http://www.cnblogs.com/ligongzi/archive/2012/08/24/2654448.html

Look at the structure of the three days, it is time to summarize.

Statement about the struct:

  

struct student{    char name[20];    char sex;    int age;    Char addr[40];};/ * Then define a Student type of Student variable */struct Student Student;

Maybe I was influenced by Java, and I wrote it this way:

struct man{    int age =;    int score = 80;}; int main () {man    man1  = {20,70};}    

The result is that the compiler does not pass. Because this is my own declaration. The compiler has not seen a struct with a default value. Struct member variables cannot be assigned to a value in a declaration.

The correct wording is:

struct man{    int age;//This is good    int score;} ; int main () {   struct man man1 = {30,80};}

It is obviously cumbersome to write structs every time you define a struct, and the refined C language is used for typedef to define use:

typedef struct man{    int age;    int score;} Man;int Main () {man    man1 = {20,80};    Man man2 = {30,70};    Man man3 = {40,99};    printf ("%d\n", man1.age);    printf ("%d\n", Man3.score);}

So there's no problem with how many "men" you want to summon. There is also an extreme point where declaring struct names and defining struct-body variable names can be the same? We can try it:

typedef struct man{    int age;    int score;} Man;  Still called man, do you have a problem? int main () {man man    = {40,50};//is also called man, is there a problem?    printf ("%d\t%d\n", Man.age,man.score);}

There is no problem with compiling the run. Don't believe yourself to try.

Then let's discuss the key points:

struct student{    char name[20];    char sex;    int age;    Char addr[40];};/ * Then define a Student type of Student variable */struct Student Student;

To assign a value to name and age in a defined structure, you can use the following statement:

strcpy (Student->name, "Jack");

student->age=21;

Student->name is the abbreviated form of (*student). Name.

It should be pointed out that the structure pointer is a pointer to the structure, is the first member of the structure of the initial address, so before use should be initialized to the structure pointer, that is, the allocation of the entire length of the structure of the byte space, which can be completed by the following function, still described as follows:

student= (struct string*) malloc (size of struct string);

A size of (struct string) automatically takes the byte length of a string structure, and the malloc () function defines a memory area of a structure length, and then returns its fraudulent address as a struct pointer.

Attention:

The struct variable name is not an address to the structure, which is different from the meaning of the array name, so if you need to require that the first member of the struct be the &[struct variable name].

Complex

The structure definition and structure of a consortium are broadly similar.

When a union is described, the compiler automatically produces a variable whose length is the largest variable length in the union.

A union can appear within a structure, and its members can also be structures.

For example:

struct{          int age;          char *addr;          union{               int i;               char *ch;          } x;     } Y[10];  

To access member I of Union x in a struct variable y[1], you can write:

Y[1].X.I;

To access the struct variable y[2], the string pointer of Union x is written in the first word of CH used by itself:

*y[2].x.ch;

If written as "Y[2].X.*CH;" Is wrong. It is worth noting that the *y[2].x.ch at this time is a wild pointer with no memory address assigned to it and will crash at run time.

Structs and unions are made up of a number of different data type members, but at any one time, the union only holds a selected member, and all members of the struct exist.

For different member assignments of a union, the value of the original member is not present, and the values for the different members of the struct are not affected.

Here is an example to add to the understanding of deep Union.

int main ()     {          union{/                   * defines a union */               int i;              struct{/             * Define a structure in the union */                    char first;                    char second;               } Half;          } number;          number.i=0x4241;         /* Union member Assignment *          /printf ("%c%c\n", Number.half.first, Mumber.half.second);          Number.half.first= ' a ';   /* struct member assignment in Union */          number.half.second= ' B ';          printf ("%x\n", number.i);     }

The output is:

Ab

6261

It can be seen from the above example that when I is assigned a value, its lower eight bits are the values of first and second; When first and second are assigned characters, the ASCII code for these two characters will also be the lower eight bits of I.

To write a prototype of a single linked list with a structure

#include <stdio.h>struct node {          struct node *link;          int value;}; void Main () {          struct node node1, Node2, Node3;          struct node *head;  Defines the chain header pointer          node1.value = 5;       Define internal attribute values for each node          node2.value = ten;          Node3.value =;          Head = &node1;                 The head pointer points to the address of the first node          node1.link = &node2;//pointer to the first node points to the address of the second node         node2.link = &node3;// A pointer to the second node is linked to the third node's address          node3.link = NULL;  The address of the third node is empty/          * Print output */          struct node *p;          p = head;          while (P! = NULL)          {                   printf ("%d\n", p->value);                   p = p->link;//Move pointer to next node          }       }

It is a form of a single-linked list, because he is too incomplete, but at least he already has a single-linked list of the shadow. I will improve it slowly.

Sum it up to myself, so I've sorted out what I think is important.

Summary of C structure body

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.