Struct 1 (nested)

Source: Internet
Author: User
1. nested struct
<1> pointer to struct: struct student * P; struct student student1; P = & student1; method of accessing struct variable members: p-> age; (* P ). age; sudent1.age; <2> One struct is a member of another struct.# Include <stdio. h> # include <malloc. h> # include <string. h> typedef struct item {char first [10]; char last [10]; int grade;} item; typedef struct node {item; struct node * Next;} node; void main () {node * node; node = (node *) malloc (sizeof (node); // scanf ("% s, % s, % d", item1.first, item1.last, & (item1.grade); scanf ("% s", node-> item. first); scanf ("% s", node-> item. last); scanf ("% d", & (node-> item. grade); printf ("% s, % s, % d \ n", node-> item. first, node-> item. last, node-> item. grade) ;}< 3> A struct pointer is a member of another struct.. # Include <stdio. h> # include <malloc. h> # include <string. h> typedef struct item {char first [10]; char last [10]; int grade;} item; typedef struct node {item * item; struct node * Next;} node; void main () {item * item; node * node; item = (item *) malloc (sizeof (item); node = (node *) malloc (sizeof (node); node-> item = item; gets (node-> item-> first); gets (node-> item-> last ); scanf ("% d", & (node-> item-> grade); printf ("% S, % s, % d \ n ", node-> item-> first, node-> item-> last, node-> item-> grade);} note: <1> A struct variable cannot be used as an input and output, but can only be used for each member. <2> A struct variable can be assigned to another struct variable of another type: student1 = student2; <3> the struct array is different from the array. The name of a structure is not the address of the structure. <4> when creating a linked list, the queue in main () is not defined as a pointer. To define it as a pointer, you must allocate storage space for it. 3. use pointers to process the linked list: struct student {int num; float score; stuct student * Next;}; function required to process the Dynamic Linked List: header file stdlib. h: The Void * malloc (unsigned int size) prototype with malloc () Free (); // allocate a continuous space with a length of size in the dynamic storage area, the value of this function is the starting address of an allocation domain (type: void ). If the function is successfully executed, null is returned. Void * calloc (unsigned N, unsinged size); // allocate n consecutive spaces with a length of size in the dynamic storage area, the value of this function is the starting address of an allocation domain. If the function is successfully executed, null is returned. The calloc function can open up dynamic storage space for one-dimensional arrays. Void free (void * P); // release the dynamic storage area directed by P. P is the value returned when malloc or calloc is called the last time. PTR = (int *) malloc (N * sizeof (INT); N is a variable, allowed before c99, returns a PTR = (int *) calloc (100, sizeof (INT); 4. passing Structure Information to functions <1> passing structure members <2> using structure addresses <3> passing structure as parameters

Struct 1 (nested)

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.