C language - initial order of pointer and structure

Posted by richardwarren on Fri, 11 Feb 2022 07:24:30 +0100

catalogue

 

🌲 Pointer

🌼 What is a pointer

🌼 Field pointer

🌱 Several cases of wild pointer

🌱 How to avoid wild pointer?

🌼 Pointer operation

🌼 Different representations of pointers

🌲 structural morphology

🌼 Definition of structure

🌼 Assignment of structure

🌼 Access to structure members

🌱 Dot operator   

🌱-> Operator

  🌼 Write a function to print the contents of the structure

🌱 Method 1: call by value

🌱 Method 2: address calling (space saving, recommended)

🌲 Pointer

🌼 What is the pointer

Pointer is an important part of C language concept And characteristic , but also master C language The more difficult part. Pointer is Memory address , pointer variables are variables used to store memory addresses. Under the same CPU architecture, different types of pointer variables occupy the same length of storage units, while variables storing data occupy different lengths due to different types of data storage space The length is also different.

In C/C + + language, pointer is generally regarded as pointer variable. The content of pointer variable stores the first address of the object it points to. The object it points to can be variable (pointer variable is also variable), array, function and other entities occupying storage space.

-- from "Baidu Encyclopedia"

Simply put, a pointer is a variable that holds the address of a variable.

🌼 Field pointer

🌱 Several cases of wild pointer

 

  1. Local variables are not initialized and default to random values
  2. Pointer out of bounds will also cause wild pointer
  3. The space pointed to by the pointer is released

🌱 How to avoid wild pointer?

1. Initialize to null pointer (when you don't know what address to initialize at present)

int* p=NULL;

Initialization pointer (clearly know the initialization value)

int a=1;int *p=&a;

2. Be careful that the array is out of bounds

3. The pointer points to the space release and is set to null in time

4. Check the validity of the pointer before use

🌼 Pointer operation

  • Pointer+-

You can perform P + +, P --, p + i and other operations on the pointer variable p, and the result is also a pointer, but the memory address pointed to by the pointer moves forward or backward by i operands compared with the memory address pointed to by P.

  • Pointer pointer (get the number of elements between pointers)
  • Relational operation of pointer (address comparison size)

🌼 Different representations of pointers

Note: [] is an operator 2 and arr are two operands

int arr[10]={1,2,3,4,5,6,7,8,9,10}int* p=arr;printf("%p
",2[arr]);printf("%p
",arr[2]);

arr[2] --> *(arr+2) -->*(2+arr) --> 2[arr]

arr[2] <==> *(arr+2) <==> *(p+2) <==> *(2+p) <==> *(2+arr) <==> 2[arr]

🌲 structural morphology

In C language, structure refers to a kind of data structure, which is a kind of composite data type in C language. Structures can be declared as variables, pointers or arrays to implement more complex data structures. A structure is also a collection of elements. These elements are called members of the structure, and these members can be of different types. Members are generally accessed by name

Reference: zh. wikipedia. Org / zh Hans / structure_ (C language)

Simply put, a structure is a collection of values, which are called member variables, and their types can be different.

🌼 Definition of structure

struct structure name

{

Member variables;

}Structural variable 1, structural variable 2// global variable

int main()

{

struct structure name member// Created an object (local variable)

}

🌼 Assignment of structure

struct structure name {member name = {member variable 1, member variable 2, member variable 3};

Use {} for assignment.

struct Stu{//Member variable char name[20]// Name int age// Age char id[20]// Student number}; int main() {/ / assign struct Stu s = {"Tom",18,"1050602040"} while initializing// Create an s variable -- local variable return 0;}

🌼 Access to structure members

🌱 Dot operator   

#include<stdio. h> Struct stu {/ / member variable char name[20]; / / name int age; / / age char id[20]; / / student number} S1, s2// S1 and s2 are also created structural variables - the global variable int main() {/ / is initialized and assigned struct Stu s = {"Tom",18,"1050602040"}// Create an s variable -- local variable printf ('% s', s.name); return 0;}

🌱-> Operator

struct Stu{//Member variable char name[20]// Name int age// Age char id[20]// Student number}s1, s2// S1 and s2 are also created structural variables - the global variable int main() {/ / is initialized and assigned struct Stu s = {"Tom",18,"1050602040"}// Create an s variable -- local variable struct stu * PS = & S// Three call methods printf ('% s')
", s.name);printf("%s
", (*ps).name);printf("%s
", ps->name);//First find the member s, and then find the member variable namereturn 0;}

It can be seen that the results of these three operations are the same.

  🌼 Write a function to print the contents of the structure

🌱 Method 1: call by value

//Method 1: pass the value and call void print1(struct Stu t) / / the function {printf ('% s') to print the content of the structure
 %d
 %s
", t.name, t.age, t.id);}int main(){//The rest are the same as above print1(s);}

🌱 Method 2: address calling (space saving, recommended)

//Method 2: address calling void print2(struct Stu* ps) / / address calling {printf ('% s')
 %d
 %s
", ps->name, ps->age, ps->id);}int main(){//Other codes are the same as above print2 & S;}

 

 

 

 

Topics: C