当前课程知识点:C Programming >  Chapter 8 Pointer >  8.1 Pointer Concept, Definition and Reference of Pointer Variables >  8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4

返回《C Programming》慕课在线视频课程列表

8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4在线视频

下一节:【Source program】Example 8.1

返回《C Programming》慕课在线视频列表

8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4课程教案、知识点、字幕

大家好,我是云南大学信息学院丁海燕老师

欢迎走进C语言程序设计课堂

今天我们讲解指针概念、指针变量的定义和引用

指针是什么?

如果在程序中定义了一个变量

在对程序进行编译时,系统就会给该变量分配

内存单元编译系统根据程序中定义的变量类型

分配一定长度的空间

例如,VC++为整型变量分配4个字节

对单精度浮点型变量分配4个字节

对字符型变量分配1个字节

内存区的每一个字节有一个编号,这就是“地址”

它相当于旅馆中的房间号。

在地址所标识的内存单元中存放数据

这相当于旅馆房间中居住的旅客一样。

由于通过地址能找到所需的变量单元

我们可以说,地址指向该变量单元。

将地址形象化地称为“指针”

务必弄清楚存储单元的地址和存储单元的内容

这两个概念的区别,例如:

int i=3,j=6,k;

printf(“%d”,i);

假设程序已定义了3整型变量i,j,k

程序编译时,可能分配地址2000~2003的

4个字节给变量i,2004~2008的4个字节给变量j

2008~2011的4个字节给k.如图所示.

程序中一般是通过变量名来引用变量的值

实际上,是通过变量名i,找到i的存储单元的地址2000

从而从存储单元读取3。

程序经过编译以后,已经将变量名转换为变量的地址

对变量值的存取都是通过地址进行的

若有语句k=i+j;

则从2000~2003字节取出i的值3

再从2004~2007字节取出j的值6

将它们相加后再将其和9送到

k所占用的2008~2011字节单元中。

这种直接按变量名进行的访问

称为“直接存取(访问)”方式。

还可以采用另一种称之为“间接访问”的方式

即将变量i的地址存放在另一变量中

然后通过该变量来找到变量i的地址,从而访问i变量。

在C语言中,可以定义整型变量、实型变量

字符变量等,也可以定义这样一种特殊的变量,用它存放地址。

假设定义了一个变量i_pointer

用来存放整型变量的地址,可以通过下面语句

将i的地址(2000)存放到i_pointer中。

i_pointer=&i;

这时,i_pointer的值就是2000

(即变量i所占用单元的起始地址)

要存取变量i的值,既可以用直接访问的方式

也可以采用间接访问的方式

先找到存放“变量i的地址”的变量i_pointer

从中取出i的地址(2000),然后到2000字节开始的

存储单元中取出i的值(3),如图所示。

图a表示直接访问,根据变量名直接向变量i赋值

由于变量名与变量的地址有一一对应的关系

因此就按此地址直接对变量i的存储单元进行访问。

图b表示间接访问,先找到存放变量i的地址的变量

i_pointer,从中得到变量i的地址(2000)

从而找到变量i的存储单元,然后对它进行存取访问。

•为了表示将数值3送到变量中,可以有两种表达方法:

(1) 将3直接送到变量i所标识的单元中,例如:i=3;

(2) 将3送到变量i_pointer所指向的单元

(即变量i的存储单元)

例如:*i_pointer=3; 其中*i_pointer表示i_pointer指向的对象

•指向就是通过地址来体现的

–假设i_pointer中的值是变量i的地址(2000)

–这样就在i_pointer和变量i之间建立起一种联系

即通过i_pointer能知道i的地址

从而找到变量i的内存单元

由于通过地址能找到所需的变量单元

因此说,地址指向该变量单元

将地址形象化地称为“指针”

意思是通过它能找到以它为地址的内存单元

•一个变量的地址称为该变量的“指针”

例如,地址2000是变量i的指针

如果有一个变量专门用来存放另一变量的地址

(即指针),则它称为“指针变量”

i_pointer就是一个指针变量

指针变量就是地址变量,用来存放地址的变量

指針变量的值是地址(即指针)

“指针”和“指针变量”是不同的概念

可以说变量i的指针是2000,而不能说i的指针变量是2000

指针是一个地址,而指针变量是存放地址的变量

从上节已知,存放地址的变量是指针变量

它用来指向另一个对象(如变量、数组、函数等)

那么怎样定义和使用指针变量呢?

下面来看,例8.1 通过指针变量访问整型变量

解题思路:先定义2个整型变量

再定义2个指针变量,分别指向这两个整型变量

通过访问指针变量,可以找到它们所指向的变量

从而得到这些变量的值

程序如下

{ int a=100,b=10;

int *pointer_1, *pointer_2; //定义两个指针变量

pointer_1=&a; //使pointer_1指向a

pointer_2=&b; //使pointer_2指向b

用printf(“a=%d,b=%d ”,a,b); //直接输出变量a和b的值

用printf输出*pointer_1和*pointer_2的值

这是间接输出变量a和b的值

int *pointer_1, *pointer_2;

此处*与类型名在一起

此时共同定义指针变量

printf(“*pointer_1=%d,*pointer_2=%d ”

此处*与指针变量一起使用

此时代表指针变量所指向的变量

定义指针变量的一般形式为:

类型名 * 指针变量名;

如:int *pointer_1, *pointer_2;

int是为指针变量指定的“基类型”

基类型指定指针变量可指向的变量类型

如pointer_1可以指向整型变量

但不能指向浮点型变量

下面都是合法的定义和初始化:

但是pointer_3=2000; 错误

*pointer_1=&a; 错误

pointer_1=&a; 正确

pointer_3=&a; 错误

定义指针变量时要注意:

1.指针变量前面的”*”表示该变量的类型为指针型变量。

2.在定义指针变量时必须指定基类型。

3.如何表示指针类型

指向整型数据的指针类型表示为int *,

读作“指向int的指针”或简称”int指针”

4. 指针变量中只能存放地址,即指针

不要将一个整数赋给一个指针变量,如

*pointer_1=100;错误

在引用指针变量时,可能有三种情况:

给指针变量赋值,如:p=&a; 使p指向a

引用指针变量指向的变量,例如

p=&a; *p=1; (*p相当于a)

则执行printf(“%d”,*p); 将输出1

引用指针变量的值,如:printf(“%o”,p);

以八进制输出a的地址

要熟练掌握两个有关的运算符

(1) & 取地址运算符

&a是变量a的地址

(2) * 指针运算符或“间接访问”运算符

如果: p指向变量a,则*p就代表a

k=*p; (把a的值赋给k)

*p=1; (把1赋给a)

下面来看,例8.2 输入a和b两个整数

按先大后小的顺序输出a和b

解题思路:用指针方法来处理这个问题

不交换整型变量的值,而是交换两个指针变量的值

程序如下:

{ int *p1,*p2,*p,a,b; p1,p2的类型是int *

输入两个整数

使p1指向a,使p2指向b

如果a小于b

则交换p1与p2的值

输出a,b

输出p1和p2所指向的变量的值

{ p=p1; p1=p2; p2=p; } 交换p1与p2的值

printf(“a=%d,b=%d ”,a,b); 输出a,b的值

printf(“%d,%d ”,*p1,*p2); 输出p1和p2所指向的变量的值9和5

下面来思考 { p=p1; p1=p2; p2=p; } 可否改为p1=&b; p2=&a;

注意:

a和b的值并未交换,它们仍保持原值

但p1和p2的值改变了,p1的值原为&a

后来变成&b,p2原值为&b,后来变成&a

这样在输出*p1和*p2时,实际上是

输出变量b和a的值,所以先输出9,然后输出5

好了,同学们,指针的概念、指针变量的定义

和引用我们就学习到这儿,下节课再见!

C Programming课程列表:

Chapter 1 Introduction

-1.1 The development and characteristics of C language

--1.The development and characteristics of C language .mp4

--1.1 Self test questions

-1.2 A simple C Language program

--1.2 A simple C Language program.mp4

--Discussion unit

--【Source program】 Example 1.1 output a line of text Hello, world! "

--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers

--1.2 Self test questions

-1.3 Program, Programming Language and C Program Running Steps

--1.3 Program, Programming Language and C Program Running Steps.mp4

--Discussion unit

--Running steps of C source program in CodeBlocks

--1.3 Self test questions

-Course references

-chapter 1 Self-Test

Chapter 2 Algorithm

-2.1 The Concept and Description of Algorithms

--2.1 The Concept and Description of Algorithms.mp4

--Discussion unit

--2.1 Self test questions

-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming

--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4

--Discussion unit

--2.2 Self test questions

-Chapter 2 Self test questions

Chapter 3 Programming in C

-3.1 Simple Structure and Identifier of C Language Program

--3.1 Simple Structure and Identifier of C Language Program.mp4

--【Source program】 Example 3.1 input two integers and output the sum of two numbers.

--【Source program】 Example 3.2 input two integers and output the average value.

--【Source program】 Example 3.3 output the value of the character variable.

--3.1 Self test questions

-3.2 Constants, Variables and Assignments

--3.2 Constants, Variables and Assignments.mp4

--Discussion unit

--3.2 Self test questions

-3.3 Arithmetic, assignment, increment and decrement operators

--3.3 Arithmetic, assignment, increment and decrement operators.mp4

--3.3 Self test questions

-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data

--【Source program】 example of sizeof

--3.4 Self test questions

-3.5 Input and Output Examples and Character Input and Output

--3.5 Input and Output Examples and Character Input and Output.mp4

--【Source program】 Example. Find the root of quadratic equation of one variable.

--【Source program】 Example 1. Successively output three characters of BOY.

--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen

--3.5 Self test questions

-3.6 Formatted output printf function

--3.6 Formatted output printf function.mp4

--【Source program】 Example 3.4 output of integer data.

--【Source program】 Example 3.5 real data output.

--【Source program】 Example 3.6 character data output.

--【Source program】 Example 3.7 uses %s to output a string.

--3.6 Self test questions

-3.7 Formatted input scanf function

--3.7 Formatted input scanf function.mp4

--Discussion unit

--【Source program】 Example 3.8 Input and output integer data

--【Source program】Example 3.9 Input and output single precision and double precision real data.

--【Source program】Example 3.10 input and output character data.

--【Source program】Example 3.11 input and output string.

--3.7 Self test questions

-3.8 Basic Data Types of C Language

--3.8 Basic Data Types of C Language.mp4

--3.8 Self test questions

-Chapter 3 Self test questions

-Operator and expression self test questions

Chapter 4 Selection Structure

-4.1 Relational operators, logical operators, and if statements

--4.1 Relational operators, logical operators, and if statements.mp4

--Discussion unit

--【source example】Example 4.1 Find the root of the quadratic equation of one variable.

--【Source program】Example 4.2 Input two real numbers and output them from small to large.

--4.1 Self test questions

-4.2 Switch Statement

--4.2 Switch Statement .mp4

--Discussion unit

--【Source program】Example 4.3 uses switch statement to implement simple menu program

--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.

--4.2 Self test questions

-4.3 Examples of Selection Structural Programs

--4.3 Examples of Selection Structural Programs.mp4

--【Source program】 Example 4.5 determines whether a year is a leap year.

--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.

--【Source program】Example 4.7 calculate the transportation cost for the user.

--4.3 Self test questions

-Chapter 4 Self test questions

Chapter 5 Loop Structure

-5.1 While and Do-While Statement

--5.1 While and Do…while statement.mp4

--Discussion unit

--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100

--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100

--5.1 Self test questions

-5.2 For Statement

--5.2 for statement.mp4

--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100

--5.2 Self test questions

-5.3 Change the State of Loop Execution and Nested Loop

--5.3 Change the state of loop execution and nested loop .mp4

--【Source program】example 5.4 collecting charity donations

--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.

--【Source program】multiplication table

--【Source program】example 5.6 Output the following 4*5 matrix.

--5.3 Self test questions

-5.4 Loop Structure Program example 1

--5.4 Loop structure program example 1.mp4

--【Source program】example 1

--【Source program】example 2

--【Source program】example 3

--【Source program】 Example 4

--5.4 Self test questions

-5.5 Loop Structure Program example 2

--5.5 Loop structure program example 2.mp4

--【Source program】example 5. Output the following figure.

--【Source program】example6. The problem of 100 chickens and 100 coins

--【Source program】example 7. Find the approximate value of PI

--5.5 Self test questions

-Chapter 5 Self test questions

Chapter 6 Batch Data Processing with Array

-6.1 Definition, Reference and Initialization of One-Dimensional Arrays

--6.1 Definition, Reference of One-Dimensional Arrays.mp4

--Discussion unit

--6.1 Self test questions

-6.2 One-Dimensional Array Programming

--6.2 One-Dimensional Array Programming.mp4

--【Source program】example 1. Fibonacci sequence

--【Source program】example 2. Find the maximum value and the minimum value.

--【Source program】example 3 exchanging array elements in reverse order.

--【Source program】example 4. Bubble sorting.

--6.2 Self test questions

-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays

--6.3 Definition, Reference of Two-Dimensional Arrays.mp4

--6.3 Self test questions

-6.4 Two-Dimensional Array Programming

--6.4 Two-Dimensional Array Programming.mp4

--【Source program】example 1. Get the average score of each subject.

--【Source program】Example 2. The elements of row and column of a 2D array are interchanged

--6.4 Self test questions

-6.5 Definition, initialization and input and output of character arrays

--6.5 Definition, initialization and input and output of character arrays .mp4

--Discussion unit

--【Source program】Example 6.6 Output a known string.

--【Source program】Example 6.7 Output a diamond.

--【Source program】Example 6.10 Sorting of strings.

--6.5 Self test questions

-6.6 String Processing Function

--6.6 String Processing Function.mp4

--6.6 Self test questions

-6.7 Character Array Programming

--6.7 Character Array Programming.mp4

--6.7 Self test questions

-Chapter 6 Self test questions

Chapter 7 Modular Programming with Functions

-7.1 Function Concept and How to Define and Call Functions

--7.1 Function Concept and How to Define and Call Functions.mp4

--【Source program】Example 7.1

--7.1 Self test questions

-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value

--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4

--Discussion unit

--【Source program】Example 7.2

--7.2 Self test questions

-7.3 Declarations of called functions and nested calls to functions

--7.3 Declarations of called functions and nested calls to functions .mp4

--【Source program】Example 7.4 use a function to find the sum of t

--7.3 Self test questions

-7.4 Recursive call to function

--7.4 Recursive call to function.mp4

--【Source program】Example 7.6. How old is the fifth student?

--【Source program】Example 7.7 Use recursion method to find n!

--7.4 Self test questions

-7.5 Array as function parameter (1)

--7.5 Array as function parameter (1).mp4

--Discussion unit

--【Source program】Example 7.10

--7.5 Self test questions

-7.6 Array as function parameter (2)

--7.6 Array as function parameter (2).mp4

--【Source program】Selection sorting.

--【Source program】Example 7.13 find the maximum of a 3×4 matrix.

--7.6 Self test questions

-7.7 Local and global variables, internal and external functions

--7.7 Local and global variables, internal and external functions.mp4

--【Source program】Example 7.14

--7.7 Self test questions

-7.8 The Survival Period of Variables and the Storage Mode of Local Variables

--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4

--【Source program】Example 7.17

--7.8 Self test questions

-7.9 Storage Categories of Global Variables

--7.9 Storage Categories of Global Variables.mp4

--7.9 Self test questions

-Chapter 7 Self test questions

Chapter 8 Pointer

-8.1 Pointer Concept, Definition and Reference of Pointer Variables

--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4

--【Source program】Example 8.1

--8.1 Self test questions

--Discussion unit

-8.2 Pointer variables as function parameters

--8.2 Pointer variables as function parameters.mp4

--Discussion unit

--【Source program】Example 8.3. Exchange two data with function call.

--8.2 Self test questions

-8.3 Pointer of array element, operation of pointer and reference of array element by pointer

--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4

--【Source program】Point to array elements with pointer variables.

--8.3 Self test questions

-8.4 Using Array Name as Function Parameter

--8.4 Using Array Name as Function Parameter.mp4

--【Source program】Store array elements in reverse order.

--8.4 Self test questions

-8.5 Reference to multidimensional arrays by pointers

--8.5 Reference to multidimensional arrays by pointers.mp4

--【Source program】Pointer variable pointing to one-dimensional array.

--8.5 Self test questions

-8.6 Referencing strings through pointers

--8.6 Referencing strings through pointers.mp4

--【Source program】Reference string by pointer.

--8.6 Self test questions

-8.7 Character pointer as function parameter

--8.7 Character pointer as function parameter.mp4

--【Source program】Character pointer as function parameter.

--8.7 Self test questions

-8.8 Pointer pointing to function

--8.8 Pointer pointing to function.mp4

--【Source program】Pointer variable pointing to the function.

--8.8 Self test questions

-8.9 Functions that return pointer values

--8.9 Functions that return pointer values.mp4

--【Source program】Intercept substring.

--8.9 Self test questions

-8.10 Pointer arrays and multiple pointers

--8.10 Pointer arrays and multiple pointers.mp4

--【Source program】Sorting of strings.

--8.10 Self test questions

-8.11 Dynamic memory allocation and pointer variables pointing to it

--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4

--Discussion unit

--【Source program】Dynamic memory allocation.

--8.11 Self test questions

-Chapter 8 Self test questions

Chapter 9 Structure

-9.1 Define and use structural variables

--9.1 Define and use structural variables.mp4

--【Source program】Structure variable

--9.1 Self test questions

-9.2 Using structure arrays

--9.2 Using structure arrays.mp4

--Discussion unit

--【Source program】Structure array

--9.2 Self test questions

-9.3 Structure pointer

--9.3 Structure pointer.mp4

--【Source program】Structure pointer

--9.3 Self test questions

-Chapter 9 Self test questions

CodeBlocks Baidu online disk download

-CodeBlocks Baidu online disk download address

Final Exam

-Final Exam

--final exam

8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4笔记与讨论

也许你还感兴趣的课程:

© 柠檬大学-慕课导航 课程版权归原始院校所有,
本网站仅通过互联网进行慕课课程索引,不提供在线课程学习和视频,请同学们点击报名到课程提供网站进行学习。