Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study of C language structure

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "C language structure case Analysis". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this article "C language structure case Analysis" can help you solve the problem.

1. Dynamic memory management

C language code-> compile-> Link-> how is the data in the executable binary file (xxx.exe under windows) arranged? Text segment, static segment, global segment.

Stack space: the space that code has when it is running. Stack space: the system is responsible for application and release. For example: functional parameters, arrays, etc. Heap space: the programmer is responsible for the application and release.

# include / / Standard library header file void * malloc (int size); / / memory request. Parameter indicates the size of the applied space. The returned value: the address of the requested space void free (void * p); / / memory release. The parameter is the first address of the space to be freed.

Example of a dynamic space application.

Dynamic space application # include "stdio.h" # include "string.h" # include int main () {int * p=malloc (sizeof (int)); / / Application space if (paired null) {printf ("applied space address: 0x%X\ n", p); * pendant 888; printf ("% d\ n", * p);} free (p) / / Free space return 0;} example 2: # include "stdio.h" # include "string.h" # include char* func (void) {char*str=malloc (100); / / char str [100]; if (stringency null) {strcpy (str, "1234567890"); printf ("subfunction print:% s\ n", str); / / free (str) / / Free space return str;} else {return NULL;}} int main () {char * p=func (); printf ("main function print:% s\ n", p); return 0;} 2. Structure 2.1 definition syntax

The concept of structure: a collection that can store different data types. For example: store the information of the students in a class. You can use a structure to store a student's information. A structural array stores the learning information of the entire class. The concept of array: a collection that can hold the same data type.

Definition syntax of the structure:

/ / declare a new type-data type struct {1; 2;. }; / / ends with a semicolon ending struct MyStruct {char a; int b; float c; char str [100];}; 2.2 definition example

How is the structure assigned? How to access internal members of a structure

# include "stdio.h" # include "string.h" # include / / defines the structure data type struct MyStruct {char a; int b; float c; char str;}; int main () {struct MyStruct data= {'Agglement journal 123456.789, "abcd"}; / / data is the syntax for structure type variables / / structure variables to access internal members:. Dot operator printf ("% c\ n", data.a); printf ("% d\ n", data.b); printf ("% f\ n", data.c); printf ("% s\ n", data.str); return 0;} 2.3 initialization # include "stdio.h" # include "string.h" # include / / defines the structure data type struct MyStruct {char a; int b; float c Char str [100];} data= {'Aggle journal 123456.789, "abcd"}; / / data is the structure type variable int main () {/ / structure variable access internal member syntax:. Dot operator printf ("% c\ n", data.a); printf ("% d\ n", data.b); printf ("% f\ n", data.c); printf ("% s\ n", data.str); return 0;} 2.4 structure assignment / / syntax for structure variable access to internal members:. The dot operator # include "stdio.h" # include "string.h" # include / / defines the structure data type struct MyStruct {char a; int b; float c; char str;}; int main () {struct MyStruct data;//data is the variable / / member of the structure type assigned to data.a='A'; data.b=123; data.c=456.789 separately Strcpy (data.str, "abcd"); / / Array assignment / / structure variable access internal member syntax:. Dot operator printf ("% c\ n", data.a); printf ("% d\ n", data.b); printf ("% f\ n", data.c); printf ("% s\ n", data.str); return 0 Structure assignment is divided into two standards: C89, C99  structure array # include "stdio.h" # include "string.h" # include / / defines the structure data type struct MyStruct {char a; int b; float c; char str;}; int main () {struct MyStruct data [100]; / / data is the structure array type variable struct MyStruct data2 [50] / / members assign individual values data [0] .astatAids; data [0] .b = 123; data [0] .c = 456.789; strcpy (data [0] .str, "abcd"); / / array assignment / / syntax for structure variables to access internal members:. Dot operator printf ("% c\ n", data [0] .a); printf ("% d\ n", data [0] .b); printf ("% f\ n", data [0] .c); printf ("% s\ n", data [0] .str); return 0 Structure pointer assignment # include "stdio.h" # include "string.h" # include / / defines the structure data type struct MyStruct {char a; int b; float c; char str;}; int main () {/ / struct MyStruct buff [100]; / / struct MyStruct * data=buff; / / structure pointer type variable struct MyStruct * data=malloc (sizeof (struct MyStruct)) Data- > axioms Aids; data- > bread123; data- > caches 456.789; strcpy (data- > str, "abcd"); / / the structure pointer accesses the variable pass-> operator of the internal member. Printf ("% c\ n", data- > a); printf ("% d\ n", data- > b); printf ("% f\ n", data- > c); printf ("% s\ n", data- > str); return 0;} 3. Student management system

Assignment: student management system

Requirements: (each function is encapsulated with a function) 1. Realize the input of student information from the keyboard. (name, gender, student number, grade, telephone number) 2. Print out all the student information in the structure. 3. To find the students according to their names or student numbers, and print out the specific information of the students after finding them. 4. Sort the student information according to the students' grades. 5. Delete the student information according to the student number.

Example:

# include "stdio.h" # include "string.h" # include / / define the structure type struct StuDentInfo {char Name [20]; / / name int number; / / student number char phone [20]; / / phone number}; / / global variable area unsigned int StuDentCnt=0; / / record the number of all students entered / / function declaration area void PrintStuDentInfoList (void) Void InputStuDentInfo (struct StuDentInfo*info); void FindStuDentInfo (struct StuDentInfo*info); void SortStuDentInfo (struct StuDentInfo*info); void PrintStuDentInfo (struct StuDentInfo*info); int main () {struct StuDentInfo data [100]; / / Information of 100 students int number; while (1) {PrintStuDentInfoList (); / / print function list scanf ("% d", & number) Printf ("\ n"); switch (number) {case 1: InputStuDentInfo (data); break; case 2: FindStuDentInfo (data); break Case 3: SortStuDentInfo (data); break; case 4: PrintStuDentInfo (data); break; case 5: break Default: printf ("wrong choice!\ n\ n"); break;}} return 0 } / * function function: print the function list of student management system * / void PrintStuDentInfoList (void) {printf ("\ n-function list of student management system -\ n"); printf ("1. Enter student information\ n "); printf (" 2. Find the student information according to the student number\ n "); printf (" 3. Sort by student number\ n "); printf (" 4. Print all student information\ n "); printf (" 5. Delete specified student information\ n "); printf (" Please select function serial number: ");} / * function function: enter student information * / void InputStuDentInfo (struct StuDentInfo*info) {printf (" enter student name: "); scanf ("% s ", info[ StuDentCnt] .Name); printf (" enter student number: "); scanf ("% d ", & info[ StuDentCnt] .number) Printf ("enter phone number:"); scanf ("% s", info [StuDentCnt] .phone); StuDentCnt++; / / quantity self-increasing} / * function: find student information * / void FindStuDentInfo (struct StuDentInfo*info) {int num,i; printf ("enter student number:"); scanf ("% d", & num); for (iTun0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report