C language structure and the use of structure pointer
tips: Recently, we need to learn about data structures, so what is essential is the structure and structure pointer of c language, so we'll sort out these basic knowledge
c language structure (struct) is a data set composed of a series of data with the same type or different types. Generally speaking, it is a collection. c language is a process oriented programming language, and the use of structure is a little similar to object-oriented on some levels. Let's return to the topic and learn the knowledge of structure and structure pointer.
1, Definition and initialization of structure variables
1. First, let's look at the definition of structure
struct Structure name { Member list; };//Don't miss the semicolon here!
- 1
- 2
- 3
- 4
//Definition of structure struct student { int num; char name[20]; char sex; };
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Here, we need to distinguish between structure name and variable name. Structure name is a type and cannot be used as variable name
Variable name declaration method:
1) struct structure name structure variable name;
2) Add the variable name declaration directly when defining the structure
//Define and initialize in main() struct student st1 = {1001,"wr",'m'};
- 1
- 2
//Structure definition and initialization struct student { int num; char name[20]; char sex; }st2 = { 1002,"wr",'m' };
- 1
- 2
- 3
- 4
- 5
- 6
- 7
//Initializing structure variables with scanf() method struct student st3; scanf("%d %s %c",&st3.num,st3.name,&st3.sex);//For arrays, scanf input does not use "&"
- 1
- 2
- 3
Access (output) of variables of structure
printf("%d,%s,%c\n", st1.num, st1.name, st1.sex);
- 1
2. Definition and initialization of structure array
struct student sarr[3];//Define structure array //Loop initialization for (int i = 0; i < 3; i++) { scanf("%d %s %c",&sarr[i].num,sarr[i].name,&sarr[i].sex); } //Test output for (int i = 0; i < 3; i++) { printf("%d,%s,%c\n", sarr[i].num,sarr[i].name,sarr[i].sex); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2, Use of structure pointer
1. Definition method of structure pointer
1) struct structure name * pointer;
2) Directly add the declaration of structure pointer when defining structure
//Defined in the main() method struct student *p1;//Define structure pointer
- 1
- 2
struct student { int num; char name[20]; char sex; }*p2;
- 1
- 2
- 3
- 4
- 5
- 6
2. Access structure variable (output) using structure pointer
Access variable method of structure pointer
1) P - > structure member;
2) (* p). Structural member;
//Pointer access struct student *p;//Define structure pointer p=&st1; printf("%d %s %c\n",p->num,p->name,p->sex);
- 1
- 2
- 3
- 4
//Pointer access struct student *p;//Define structure pointer p=&st1; printf("%d %s %c\n", (*p).num, (*p).name, (*p).sex);
- 1
- 2
- 3
- 4
After reading the above basic knowledge, do you think it is relatively simple:), let's analyze two special structure pointer access methods:)
1)p->num++
2)p++ ->num
Let's look at the following example
You can guess the output without looking at the comments:)
#include <stdio.h> #include <stdlib.h> //Define structure struct student { int num; char name[20]; char sex; };
int main()
{
//Define structure array
struct student sarr[3] = {
1001,"wr1",'m',1003,"wr2",'m',1005,"wr3",'m' };
struct student *p;// Define structure pointer
p <span class="token operator">=</span> sarr<span class="token punctuation">;</span><span class="token comment">//The array itself is an address. You don't need to take an address</span> <span class="token keyword">int</span> num<span class="token punctuation">;</span> num <span class="token operator">=</span> p<span class="token operator">-></span>num<span class="token operator">++</span><span class="token punctuation">;</span><span class="token comment">//num=p-> num; p-> num=p-> Num + 1, priority - & gt; Priority of is higher than++</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"num=%d,p->num=%d\n"</span><span class="token punctuation">,</span>num<span class="token punctuation">,</span>p<span class="token operator">-></span>num<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//p-> Num has been added by one in the previous step</span> num <span class="token operator">=</span> p<span class="token operator">++</span><span class="token operator">-></span>num<span class="token punctuation">;</span><span class="token comment">//num=p->num,p=p+1</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"num=%d,p->num=%d\n"</span><span class="token punctuation">,</span> num<span class="token punctuation">,</span> p<span class="token operator">-></span>num<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
Let's take a look at the output
Look, you guessed right...
Attachment: priority of common operators in c language
priority | operator | meaning | Combination direction |
---|---|---|---|
1 | [] | Array subscript | From left to right |
() | Parentheses | ||
. | Member selection (object) | ||
-> | Member selection (pointer) | ||
2 | - | minus sign | Right to left |
~ | Bitwise inversion | ||
++ | Self increasing | ||
-- | Self subtraction | ||
* | Value | ||
& | Get address | ||
! | Logical non | ||
sizeof | Data type length | ||
(type) cast | Cast type | ||
3 | / | except | From left to right |
* | ride | ||
% | Take mold | ||
4 | + | plus | From left to right |
- | reduce | ||
5 | << | Shift left | From left to right |
>> | Shift right | ||
6 | > | greater than | From left to right |
>= | Greater than or equal to | ||
< | less than | ||
<= | Less than or equal to | ||
7 | == | be equal to | From left to right |
!= | Not equal to |