C语言英文课件9:指针
C语言教学课件

{float x=67,y;
y=2*sin(x*PI/180) ;
printf(“y=%f” , y ) ;
}
29返回
第三章 数据类型、运算符与表达式
§3.3 整型数据
1、常量: 十进制 567 ,-789 ,0 八进制 0567, -0753 十六进制 0x567 , -0xa1
2、变量: int i , j ; i=10 ; j= -10 ;
1+2+3+……+100=5050 或 100+1+99+2+98+……+49+51+50= 100+49*100+50=表示算法)
起止框
输入/出框
处理框
流程线
判断框
连接点
注释框
17
第二章 算法
例2.1 计算265的平方根
开始
顺序结构 X=265
1970 B (取BCPL第一个字母)
1972 C (取BCPL第二个字母)
1978 标准C
1983 ANSI C
1987 87 ANSI C 1990 ISO C
7跳转
第一章 C语言概述
§1.2 C语言特点
1、简练、自由、结构化 (32个关键字、9种控制语句)
2、运算符丰富(附录Ⅲ)34种 3、数据结构丰富
22返回
第三章 数据类型、运算符与表达式
第三章 数据类型、运算符与表达式
§3.1
C的数据类型
基本类型
整型 实型 字符型 枚举型
单精度 双精度
C数据类型
构造类型 指针类型
数组型 结构体型 共用体型
空类型 void(无return的函数)
C语言的指针和结构体

– 如果分配成功,原存储块的内容就可能改变了,因此不
允许再通过ptr去使用它精选。课件ppt
17
结构体
精选课件ppt
18
结构体的定义
• 结构体类型定义的一般形式为:
struct 结构名 {
//关键字struct和它后
类型名
结构成员名1;
面的结构名一起组成 一个新的数据类型名
类型名 结构成员名2;
所需要的存储空间。 (3)使指针指向获得的内存空间,以便用指
针在该空间内实施运算或操作。 (4)当使用完毕内存后,释放这一空间。
精选课件ppt
13
动态存储分配函数malloc()
void *malloc(unsigned size)
在内存的动态存储区中分配一连续空间,其长度为 size
– 若申请成功,则返回一个指向所分配内存空间的起 始地址的指针
& : 取地址运算符,给出变量的地址。
int *p, a = 3;
p = &a; 把 a 的地址赋给 p,即 p 指向 a
p
a
&a
3
*p
//指针变量的类型和它所指向变量的类型相同
* :间接访问运算符,访问指针所指向的变量。
*p:指针变量 p 所指精向选课的件p变pt 量。
4
p
a
说明 int a = 3, *p;
};
name ↓
Zhang
age ↓
26
telephone ↓
精选课件ppt
23
结构体变量成员的使用
结构体变量名 .成员名
friend1.age = 26; strcpy(, "Zhang San"); strcpy(friend1.addr.zip, " 210044 ");
全的C语言指针详解PPT课件

在函数中使用指针参数
03
使用指针参数来访问和修改指针所指向的内容,需要使用“-
>”或“*”运算符。
05
指针的高级应用
指向指针的指针(二级指针)
定义与声明
二级指针是用来存储另一个指 针的地址的指针。在声明时, 需要使用`*`操作符来声明二级
指针。
初始化与使用
通过使用`&`操作符获取一个指 针的地址,并将该地址存储在 二级指针中。然后,可以通过 二级指针来访问和操作原始指
当使用malloc或calloc等函 数动态分配内存后,如果 不再需要该内存,必须使 用free函数释放它。否则, 指针将指向一个无效的内 存地址。
当一个指针在函数中定义 ,但该函数返回后仍然存 在并继续指向无效的内存 地址时,就会产生野指针 。
避免指针越界访问
总结词:指针越界访问是指试图访问数 组之外的内存,这是不安全的,可能会 导致程序崩溃或产生不可预测的结果。
指针与内存分配
通过指针来访问和操作动态分配的内存空间。指针可以 存储动态分配的内存地址,并用于读取和写入该地址中 的数据。
指向结构体的指针
01
定义与声明
指向结构体的指针是指向结构体类型的指针。在声明时,需要使用结
构体类型的名称来声明指向结构体的指针。
02 03
初始化与使用
通过使用`&`操作符获取结构体的地址,并将该地址存储在指向结构 体的指针中。然后,可以通过该指针来访问和操作结构体中的成员变 量。
```
பைடு நூலகம்
指向数组元素的指针
• 指向数组元素的指针是指向数组中某个具体元素的指针。通过将指针指向数组中的某个元素,可以访问该 元素的值。
• 指向数组元素的指针可以通过定义一个指向具体元素的指针来实现。例如,定义一个指向数组中第三个元 素的指针,可以使用以下代码
c语言中指针用法

1) 运算符 :*
该运算符作用在指针变量上,表示访问指针
变量所指向的数据对象编。辑课件
22
2)作用:
实现对指针所指向的数据对象的间接访问, 包括引用和赋值等基本运算。
例6-6 下面的程序段包含着对指针的间接访问。
int a,b=2,c,*p; …… p=&b; scanf("%d",&a); c=a+*p;
1.指针的赋值
操作指针之前必须赋予确定的值,可以在 定义指针的同时赋予初值,也可以用赋值表达 式对指针变量赋值。
(1)赋空值(NULL); (2)赋予某个变量的地址。 (3)将一个指针变量的值赋予另一指针变量。
例如:int grade,*p; 编辑课件
16
p=&grade;
2.指针的加减运算
1)运算符:+、-、++、-- 。
--:原地址减去一个地址单位(基类型的实际字 节数);
例如: int *iptr; …… iptr++;// iptr=iptr+1,向下移动两个字节 iptr--;// iptr=iptr-1,向上移动两个字节
下图给出指针的移动示编意辑课:件
19
3. 取地址运算
1) 运算符:& 2) 作用:获取数据对象的内存地址,如果是结 构数据对象则获取其内存首地址。
重点:
➢ 指针的概念 ➢ 指针运算; ➢ 内存操作函数和动态存储分配。
课堂时数:6—7学时 上机时数:2—3学时 课外上机时数:2—3学时
编辑课件
2
6.1 指针的概念
1. 什么叫指针
一个数据对象的内存地址称为该数据对象 的指针 。
软件技术基础之指针和结构体课件ppt课件

变
从右图可知,指 针变量和一般变量一 指针变量pi 变量地址(指针)
量 地 址
样,仅仅是变量旳内
指
存
向
入
容为变量地址旳变量
变量i
变量值
指
而已。这里变量和指针变量旳类型必须一致。
针 变
量
一般而言,指针变量只能存储某一种数据类型旳 变量旳地址,由此可将指针变量分为:整型指针变量、 实型指针变量和字符型指针变量等等,指针变量旳类 型表白了其指向旳存储单元中存储旳数据旳类型。
另一种引用旳形式: 构 造指针变量名 组员名 定义:struct data * today; today 为指向该构造旳指针
可见,“”和“·”操作符分别使用在用指向构造旳 指针和用构造变量来访问构造中旳数据项(组员)时。
例:构造例程。
结 构 体小结
构造体是C语言中旳一种复杂(构造)类型,它是一种 被命名为一种标识符旳多种变量旳集合。构造旳定义拟定了 构造变量或构造指针旳格式,构造是软件设计中实现数据抽 象旳基础。
3、程序中一旦定义了一种变量,该变量在内存中就将 占用一定旳存储单元。变量占用旳存储单元总是n(n>0) 个连续旳字节,开始字节旳地址,就是变量旳地址也可 称为该存储单元旳首地址。
二、指针旳基本概念:
指针就是变量旳地址。与此相应,在C语言中使
用一类专门旳变量即指针变量来存储变量旳地址。
所以,指针变量是存储地址数据旳变量。
四、指针旳“&”、“*”和赋值运算:
1 、& 取地址运算和 * 引用运算 取地址运算符&旳作用是取变量旳地址。如:
& i 表达求变量 i 旳地址。 引用运算符 * 旳作用是取指针指向变量旳内容。
C语言程序设计教程ppt课件(2024)

使用嵌套的for循环打印九九乘法表。
23
04
数组与字符串处理
2024/1/30
24
一维数组定义和操作
2024/1/30
01
一维数组定义
类型说明符 数组名
[常量表达式];
02
一维数组初始化
在定义数组时对数 组元素赋以初值;
04
一维数组操作
包括赋值、输入输
03
出、排序、查找等
。
一维数组引用
do-while循环
与while循环类似,但至少会执行一次 循环体。
示例
int i = 0; do { printf("%d ", i); i; } while (i < 10);
语法
do { // code to be executed } while (condition);
2024/1/30
19
赋值运算符
包括赋值运算符(=)和复合赋 值运算符(+=、-=、*=、/=、 %=),用于为变量赋值或进行 运算后赋值。
12
类型转换与运算顺序
2024/1/30
类型转换
当不同类型的数据进行运算时,需要进行类型转换,包括自 动类型转换和强制类型转换。自动类型转换由编译器自动完 成,而强制类型转换需要使用类型转换运算符。
0。
free函数
用于释放已分配的内存空间 ,避免内存泄漏。
2024/1/30
36
指针在数组、字符串等数据结构中应用
指针与数组
数组名本质上是一个指向数组首元素的指针;通过指针可 以访问和修改数组元素。
指针与字符串
字符串在C语言中以字符数组的形式存储,指针可以方便 地操作字符串。
C语言数据类型ppt课件

2.2 常量与变量
(3)整型数据在内存中的存放形式
int x; x=10; 则变量x的值10在内存中的存放形式:
int x;
x=-10; 则变量x的值10在内存中的存放形式:
22
2.2 常量与变量
(4)整型数据的溢出
例2-2 整型数据的溢出问题
#include <stdio.h>
int main()
\r
<
\a
\‘
回车 响铃 单引号
\ddd 3位8进制数代表的字符
转义字符 \t \b \f \\ \“ \xhh
含义 水平制表 退格 换页 反斜线 双引号 2位16进制数代表的字符
12
2.2 常量与变量
如 ‘\101’ -----------‘A’ ‘\012’ -----------’\n’ ‘\376’ -----------’’ ‘\x61’ -----------’a’ ‘\060’ -----------’0’
unsigned short。 ⅲ. 无符号长整型:类型说明符为
unsigned long。
20
2.2 常量与变量
(2)整型变量的定义与使用
例2-1 整型变量的使用
#include <stdio.h> int main() {
int x,y,z; x=10;y=20; z=x+y; printf(“x+y=%d\n”,z); return 0; } 输出结果:x+y=30
说明:数据类型所占字节数随机器硬件不同而不同, 上表以IBM PC机为例。
4
2.2 常量与变量
• 数据是程序的处理对象 • C语言将数据分为常量与变量
c语言第9章_指针及其应用(1)课后复习重点[1]教程
![c语言第9章_指针及其应用(1)课后复习重点[1]教程](https://img.taocdn.com/s3/m/9867c49ef121dd36a32d8261.png)
指针变量在使用前
必须赋值!
swap(pointer_1,pointer_2);
printf("a=%d,b=%d\n", *pointer_1,*pointer_2); return 0; } 2018/11/2
运行结果:
a:10 *pa:10
...
printf("&pa:%x(hex)\n",&pa);
return 0; }
&a:f86(hex)
pa:f86(hex) &pa:f8a(hex)
16
2018/11/2
例:输入两个数,并使其从大到小输出
int main()
{
int *p1,*p2,*p,a,b; scanf("%d,%d",&a,&b);
24
例:交换两个数—形参和实参都为指针变量
void swap(int *p1, int *p2) { int *p; int x,*p=&x; *p=*p1; *p1=*p2; *p2=*p; } int main() { int a=7,b=11; int *pointer_1=&a,*pointer_2=&b;
2018/11/2
9
例: void main( ) { int i=10; 危险! int *p; *p=i; printf("%d",*p); } 例: void main( ) { int i=10,k; int *p; p=&k; *p=i; printf("%d",*p); } 2018/11/2
C语言指针讲解ppt课件

运行情况如下: 5,9↙ a=5,b=9 max=9,min=5 当输入a=5,b=9时,由于a<b, 将p1和p2交换。交换前的情况见图 (a),交换后见图(b)。
地址。
10.2.2 怎样引用指针变量
在引用指针变量时,可能有三种情况: ⑴给指针变量赋值。如:
p=&a; ⑵引用指针变量的值。如:
printf(“%o”,p); ⑶引用指针变量指向的变量。
有关的两个运算符: (1) & 取地址运算符。 &a是变量a的地址。 (2) * 指针运算符 (或称“间接访问”运算符),*p
1 2 3 4 5 6 7 10 9 0↙
1 2 3 4 5 6 7 10 9 0
10.3.4 用数组名作函数参数
在第7章中介绍过可以用数组名作函数的参数。 如: void main()
{if(int arr[],int n); int array[10]; ┇ f(array,10); ┇ } void f(int arr[ ],int n)
{ ┇
}
例10.7 将数组a中n个整数按相反顺序存放
#include <stdio.h> void main() { void inv(int x[ ],int n);
int i,a[10]={3,7,9,11,0, 6,7,5,4,2};
printf(″The original array:\n″); for(i=0;i<10;i++) printf (″%d,″,a[i]); printf(″\n″); inv (a,10); printf(″The array has been in verted:\n″); for(i=0;i<10;i++) printf (″%d,″,a[i]); printf (″\n″); }
《C语言》指针--ppt课件全文

说明: 这种方法可能会破坏系统的正常
工作状态,因为temp是一个指针变量 b 59
但是在函数中并没有给temp一个确定 的地址,这样它所指向的内存单元是 不可预见的,而对*temp的赋值可能 带来危害
swap2 &a p1
&b p2 随机值 temp
5?
ppt课件
11
例 6. 3 ③ #include <stdio.h> void swap3( int *p1, int *p2) { int *p;
p
*p = 12 ; printf (“%d\n” , *p ) ;
对a 重新赋值 等价于 a=12
2. & 与*
p =ห้องสมุดไป่ตู้&a ;
1010 152 a
&*p &(*p) &a *&a *(&a) *p a
ppt课件
6
3. *与 ++ , - -
int a = 2 , b = 5 , c , d , *p ; (1) p = &a ;
② 形参表列: 即指针变量所指向的函数的形参表列 ③ 格式中的小括号不能省略 2. 应用 (1) 让指针变量指向函数 pt = add ; 因为函数名为函数的入口地址, 所以直接将函数名 赋给指针变量即可 (2) 使用指针变量调用函数 格式 : (*指针变量名) ( 实参表列)
ppt课件
17
例 求一维数组中全部元素的和
因此我们可以定义一个指针变量, 让它的值等于 函数的入口地址, 然后可以通过这个指针变量来调用 函数, 该指针变量称为指向函数的指针变量
ppt课件
16
指向函数的指针变量
C语言07 指针

指针变量
定义指针变量: type *name
其中: Type可以是C语言中的任何一种有效类型 指针的基类型(type)定义了该指针可以指向的变
量的类型 从技术上讲,任何类型的指针均可指向内存中的任
何地址,但实际上,所有指针的运算都与它的基类 型相关,所以正确说明指针基类型十分重要 如:int *pointer_1; pointer_1可以指向整型变量,但不能指向浮点型 等其它类型的变量
用数组作为函数参数时,就把数组的地址传 给了函数 L8_7.c;L8_score.c;L8_matchChar.c
指针与数组
指针和数组是紧密相连的。一个变量有地址, 一个数组包含若干个元素,每个元素都有相应 的地址,指针变量可以指向数组元素,即:将 某一元素的地址放入一个指针变量中
int a[10]; 数组名a其实就是一个地址值(常量),记录
p的值为2000
p+1的值为2004
P-1的值为1996
越界!
通过指针引用数组元素
引用一个数组元素,可用下面两种方法: (1) 下标法,如a[i]形式 (2) 指针法,如*(a+i)或*(p+i)
其中: a是数组名,p是指向数组元素的指针变
量,其初值 p=a
L7_4.c,L7_5.c
字符串
在地址所标识的内存单元中存放数据,相当 于在旅馆房间中居住旅客
我们将内存单元的地址称为“指针”
如:变量A存放的是另一个变量B的地址,则 称A是B的指针变量,或A指向B
内存地址 1000 1001 1002 1003 1004 …… 8000 8001 8002
内存单元的值 8001
…… 20 36 10
使用指针变量自身的值(地址)
C指针(英文)

A TUTORIAL ON POINTERS AND ARRAYS IN Cby Ted JensenVersion 1.2 (PDF Version)Sept. 2003This material is hereby placed in the public domainAvailable in various formats via/~tjensen/ptr/cpoint.htmTABLE OF CONTENTSPREFACE 2 INTRODUCTION 4 CHAPTER 1: What is a pointer? 5 CHAPTER 2: Pointer types and Arrays 9 CHAPTER 3: Pointers and Strings 14 CHAPTER 4: More on Strings 19 CHAPTER 5: Pointers and Structures 22 CHAPTER 6: Some more on Strings, and Arrays of Strings 26 CHAPTER 7: More on Multi-Dimensional Arrays 30 CHAPTER 8: Pointers to Arrays 32 CHAPTER 9: Pointers and Dynamic Allocation of Memory 34 CHAPTER 10: Pointers to Functions 42 EPILOG 53PREFACEThis document is intended to introduce pointers to beginning programmers in the C programming language. Over several years of reading and contributing to various conferences on C including those on the FidoNet and UseNet, I have noted a large number of newcomers to C appear to have a difficult time in grasping the fundamentals of pointers. I therefore undertook the task of trying to explain them in plain language with lots of examples.The first version of this document was placed in the public domain, as is this one. It was picked up by Bob Stout who included it as a file called PTR-HELP.TXT in his widely distributed collection of SNIPPETS. Since that original 1995 release, I have added a significant amount of material and made some minor corrections in the original work.I subsequently posted an HTML version around 1998 on my website at:/~tjensen/ptr/cpoint.htmAfter numerous requests, I’ve finally come out with this PDF version which is identical to that HTML version cited above, and which can be obtained from that same web site.Acknowledgements:There are so many people who have unknowingly contributed to this work because of the questions they have posed in the FidoNet C Echo, or the UseNet Newsgroupng.c, or several other conferences in other networks, that it would be impossible to list them all. Special thanks go to Bob Stout who was kind enough to include the first version of this material in his SNIPPETS file.About the Author:Ted Jensen is a retired Electronics Engineer who worked as a hardware designer or manager of hardware designers in the field of magnetic recording. Programming has been a hobby of his off and on since 1968 when he learned how to keypunch cards for submission to be run on a mainframe. (The mainframe had 64K of magnetic core memory!).Use of this Material:Everything contained herein is hereby released to the Public Domain. Any person may copy or distribute this material in any manner they wish. The only thing I ask is that if this material is used as a teaching aid in a class, I would appreciate it if it were distributed in its entirety, i.e. including all chapters, the preface and the introduction. I would also appreciate it if, under such circumstances, the instructor of such a class would drop me anote at one of the addresses below informing me of this. I have written this with the hope that it will be useful to others and since I'm not asking any financial remuneration, the only way I know that I have at least partially reached that goal is via feedback from those who find this material useful.By the way, you needn't be an instructor or teacher to contact me. I would appreciate a note from anyone who finds the material useful, or who has constructive criticism to offer. I'm also willing to answer questions submitted by email at the addresses shown below.Ted JensenRedwood City, Californiatjensen@July 1998INTRODUCTIONIf you want to be proficient in the writing of code in the C programming language, you must have a thorough working knowledge of how to use pointers. Unfortunately, C pointers appear to represent a stumbling block to newcomers, particularly those coming from other computer languages such as Fortran, Pascal or Basic.To aid those newcomers in the understanding of pointers I have written the following material. To get the maximum benefit from this material, I feel it is important that the user be able to run the code in the various listings contained in the article. I have attempted, therefore, to keep all code ANSI compliant so that it will work with any ANSI compliant compiler. I have also tried to carefully block the code within the text. That way, with the help of an ASCII text editor, you can copy a given block of code to a new file and compile it on your system. I recommend that readers do this as it will help in understanding the material.CHAPTER 1: What is a pointer?One of those things beginners in C find difficult is the concept of pointers. The purpose of this tutorial is to provide an introduction to pointers and their use to these beginners.I have found that often the main reason beginners have a problem with pointers is that they have a weak or minimal feeling for variables, (as they are used in C). Thus we start with a discussion of C variables in general.A variable in a program is something with a name, the value of which can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within the computer to hold the value of that variable. The size of that block depends on the range over which the variable is allowed to vary. For example, on PC's the size of an integer variable is 2 bytes, and that of a long integer is 4 bytes. In C the size of a variable type such as an integer need not be the same on all types of machines.When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type integer with the name k by writing:int k;On seeing the "int" part of this statement the compiler sets aside 2 bytes of memory (on a PC) to hold the value of the integer. It also sets up a symbol table. In that table it adds the symbol k and the relative address in memory where those 2 bytes were set aside.Thus, later if we write:k = 2;we expect that, at run time when this statement is executed, the value 2 will be placed in that memory location reserved for the storage of the value of k. In C we refer to a variable such as the integer k as an "object".In a sense there are two "values" associated with the object k. One is the value of the integer stored there (2 in the above example) and the other the "value" of the memory location, i.e., the address of k. Some texts refer to these two values with the nomenclature rvalue (right value, pronounced "are value") and lvalue (left value, pronounced "el value") respectively.In some languages, the lvalue is the value permitted on the left side of the assignment operator '=' (i.e. the address where the result of evaluation of the right side ends up). The rvalue is that which is on the right side of the assignment statement, the 2 above. Rvalues cannot be used on the left side of the assignment statement. Thus: 2 = k; is illegal.Actually, the above definition of "lvalue" is somewhat modified for C. According toK&R II (page 197): [1]"An object is a named region of storage; an lvalue is an expressionreferring to an object."However, at this point, the definition originally cited above is sufficient. As we become more familiar with pointers we will go into more detail on this.Okay, now consider:int j, k;k = 2;j = 7; <-- line 1k = j; <-- line 2In the above, the compiler interprets the j in line 1 as the address of the variable j (its lvalue) and creates code to copy the value 7 to that address. In line 2, however, the j is interpreted as its rvalue (since it is on the right hand side of the assignment operator '='). That is, here the j refers to the value stored at the memory location set aside for j, in this case 7. So, the 7 is copied to the address designated by the lvalue of k.In all of these examples, we are using 2 byte integers so all copying of rvalues from one storage location to the other is done by copying 2 bytes. Had we been using long integers, we would be copying 4 bytes.Now, let's say that we have a reason for wanting a variable designed to hold an lvalue (an address). The size required to hold such a value depends on the system. On older desk top computers with 64K of memory total, the address of any point in memory can be contained in 2 bytes. Computers with more memory would require more bytes to hold an address. Some computers, such as the IBM PC might require special handling to hold a segment and offset under certain circumstances. The actual size required is not too important so long as we have a way of informing the compiler that what we want to store is an address.Such a variable is called a pointer variable (for reasons which hopefully will become clearer a little later). In C when we define a pointer variable we do so by preceding its name with an asterisk. In C we also give our pointer a type which, in this case, refers to the type of data stored at the address we will be storing in our pointer. For example, consider the variable declaration:int *ptr;ptr is the name of our variable (just as k was the name of our integer variable). The '*' informs the compiler that we want a pointer variable, i.e. to set aside however many bytes is required to store an address in memory. The int says that we intend to use our pointervariable to store the address of an integer. Such a pointer is said to "point to" an integer. However, note that when we wrote int k; we did not give k a value. If this definition is made outside of any function ANSI compliant compilers will initialize it to zero. Similarly, ptr has no value, that is we haven't stored an address in it in the above declaration. In this case, again if the declaration is outside of any function, it is initialized to a value guaranteed in such a way that it is guaranteed to not point to any C object or function. A pointer initialized in this manner is called a "null" pointer.The actual bit pattern used for a null pointer may or may not evaluate to zero since it depends on the specific system on which the code is developed. To make the source code compatible between various compilers on various systems, a macro is used to represent a null pointer. That macro goes under the name NULL. Thus, setting the value of a pointer using the NULL macro, as with an assignment statement such as ptr = NULL, guarantees that the pointer has become a null pointer. Similarly, just as one can test for an integer value of zero, as in if(k == 0), we can test for a null pointer using if (ptr == NULL).But, back to using our new variable ptr. Suppose now that we want to store in ptr the address of our integer variable k. To do this we use the unary & operator and write:ptr = &k;What the & operator does is retrieve the lvalue (address) of k, even though k is on the right hand side of the assignment operator '=', and copies that to the contents of our pointer ptr. Now, ptr is said to "point to" k. Bear with us now, there is only one more operator we need to discuss.The "dereferencing operator" is the asterisk and it is used as follows:*ptr = 7;will copy 7 to the address pointed to by ptr. Thus if ptr "points to" (contains the address of) k, the above statement will set the value of k to 7. That is, when we use the '*' this way we are referring to the value of that which ptr is pointing to, not the value of the pointer itself.Similarly, we could write:printf("%d\n",*ptr);to print to the screen the integer value stored at the address pointed to by ptr;.One way to see how all this stuff fits together would be to run the following program and then review the code and the output carefully.------------ Program 1.1 ---------------------------------/* Program 1.1 from PTRTUT10.TXT 6/10/97 */#include <stdio.h>int j, k;int *ptr;int main(void){j = 1;k = 2;ptr = &k;printf("\n");printf("j has the value %d and is stored at %p\n", j, (void *)&j); printf("k has the value %d and is stored at %p\n", k, (void *)&k); printf("ptr has the value %p and is stored at %p\n", ptr, (void*)&ptr);printf("The value of the integer pointed to by ptr is %d\n", *ptr); return 0;}Note: We have yet to discuss those aspects of C which require the use of the (void *) expression used here. For now, include it in your test code. We'll explain the reason behind this expression later.• A variable is declared by giving it a type and a name (e.g. int k;)• A pointer variable is declared by giving it a type and a name (e.g. int *ptr) where the asterisk tells the compiler that the variable named ptr is a pointer variable and the type tells the compiler what type the pointer is to point to (integer in thiscase).•Once a variable is declared, we can get its address by preceding its name with the unary & operator, as in &k.•We can "dereference" a pointer, i.e. refer to the value of that which it points to, by using the unary '*' operator as in *ptr.•An "lvalue" of a variable is the value of its address, i.e. where it is stored in memory. The "rvalue" of a variable is the value stored in that variable (at thataddress).References for Chapter 1:1."The C Programming Language" 2nd EditionB. Kernighan and D. RitchiePrentice HallISBN 0-13-110362-8CHAPTER 2: Pointer types and ArraysOkay, let's move on. Let us consider why we need to identify the type of variable that a pointer points to, as in:int *ptr;One reason for doing this is so that later, once ptr "points to" something, if we write:*ptr = 2;the compiler will know how many bytes to copy into that memory location pointed to by ptr. If ptr was declared as pointing to an integer, 2 bytes would be copied, if a long, 4 bytes would be copied. Similarly for floats and doubles the appropriate number will be copied. But, defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code. For example, consider a block in memory consisting if ten integers in a row. That is, 20 bytes of memory are set aside to hold 10 integers.Now, let's say we point our integer pointer ptr at the first of these integers. Furthermore lets say that integer is located at memory location 100 (decimal). What happens when we write:ptr + 1;Because the compiler "knows" this is a pointer (i.e. its value is an address) and that it points to an integer (its current address, 100, is the address of an integer), it adds 2 to ptr instead of 1, so the pointer "points to" the next integer, at memory location 102. Similarly, were the ptr declared as a pointer to a long, it would add 4 to it instead of 1. The same goes for other data types such as floats, doubles, or even user defined data types such as structures. This is obviously not the same kind of "addition" that we normally think of. In C it is referred to as addition using "pointer arithmetic", a term which we will come back to later.Similarly, since ++ptr and ptr++ are both equivalent to ptr + 1 (though the point in the program when ptr is incremented may be different), incrementing a pointer using the unary ++ operator, either pre- or post-, increments the address it stores by the amount sizeof(type) where "type" is the type of the object pointed to. (i.e. 2 for an integer, 4 for a long, etc.).Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, this brings up an interesting relationship between arrays and pointers.Consider the following:int my_array[] = {1,23,17,4,-5,100};Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, i.e. using my_array[0] through my_array[5]. But, we could alternatively access them via a pointer as follows:int *ptr;ptr = &my_array[0]; /* point our pointer at the firstinteger in our array */And then we could print out our array either using the array notation or by dereferencing our pointer. The following code illustrates this:----------- Program 2.1 -----------------------------------/* Program 2.1 from PTRTUT10.HTM 6/13/97 */#include <stdio.h>int my_array[] = {1,23,17,4,-5,100};int *ptr;int main(void){int i;ptr = &my_array[0]; /* point our pointer to the firstelement of the array */printf("\n\n");for (i = 0; i < 6; i++){printf("my_array[%d] = %d ",i,my_array[i]); /*<-- A */printf("ptr + %d = %d\n",i, *(ptr + i)); /*<-- B */}return 0;}Compile and run the above program and carefully note lines A and B and that the program prints out the same values in either case. Also observe how we dereferenced our pointer in line B, i.e. we first added i to it and then dereferenced the new pointer. Change line B to read:printf("ptr + %d = %d\n",i, *ptr++);and run it again... then change it to:printf("ptr + %d = %d\n",i, *(++ptr));and try once more. Each time try and predict the outcome and carefully look at the actual outcome.In C, the standard states that wherever we might use &var_name[0] we can replace that with var_name, thus in our code where we wrote:ptr = &my_array[0];we can write:ptr = my_array;to achieve the same result.This leads many texts to state that the name of an array is a pointer. I prefer to mentally think "the name of the array is the address of first element in the array". Many beginners (including myself when I was learning) have a tendency to become confused by thinking of it as a pointer. For example, while we can writeptr = my_array;we cannot writemy_array = ptr;The reason is that while ptr is a variable, my_array is a constant. That is, the location at which the first element of my_array will be stored cannot be changed once my_array[] has been declared.Earlier when discussing the term "lvalue" I cited K&R-2 where it stated:"An object is a named region of storage; an lvalue is an expressionreferring to an object".This raises an interesting problem. Since my_array is a named region of storage, why is my_array in the above assignment statement not an lvalue? To resolve this problem, some refer to my_array as an "unmodifiable lvalue".Modify the example program above by changingptr = &my_array[0];toptr = my_array;and run it again to verify the results are identical.Now, let's delve a little further into the difference between the names ptr and my_array as used above. Some writers will refer to an array's name as a constant pointer. What do we mean by that? Well, to understand the term "constant" in this sense, let's go back to our definition of the term "variable". When we declare a variable we set aside a spot in memory to hold the value of the appropriate type. Once that is done the name of the variable can be interpreted in one of two ways. When used on the left side of the assignment operator, the compiler interprets it as the memory location to which to move that value resulting from evaluation of the right side of the assignment operator. But, when used on the right side of the assignment operator, the name of a variable is interpreted to mean the contents stored at that memory address set aside to hold the value of that variable.With that in mind, let's now consider the simplest of constants, as in:int i, k;i = 2;Here, while i is a variable and then occupies space in the data portion of memory, 2 is a constant and, as such, instead of setting aside memory in the data segment, it is imbedded directly in the code segment of memory. That is, while writing something like k = i; tells the compiler to create code which at run time will look at memory location &i to determine the value to be moved to k, code created by i = 2; simply puts the 2 in the code and there is no referencing of the data segment. That is, both k and i are objects, but 2 is not an object.Similarly, in the above, since my_array is a constant, once the compiler establishes where the array itself is to be stored, it "knows" the address of my_array[0] and on seeing:ptr = my_array;it simply uses this address as a constant in the code segment and there is no referencing of the data segment beyond that.This might be a good place explain further the use of the (void *) expression used in Program 1.1 of Chapter 1. As we have seen we can have pointers of various types. So far we have discussed pointers to integers and pointers to characters. In coming chapters we will be learning about pointers to structures and even pointer to pointers.Also we have learned that on different systems the size of a pointer can vary. As it turns out it is also possible that the size of a pointer can vary depending on the data type of the object to which it points. Thus, as with integers where you can run into trouble attempting to assign a long integer to a variable of type short integer, you can run into trouble attempting to assign the values of pointers of various types to pointer variables of other types.To minimize this problem, C provides for a pointer of type void. We can declare such a pointer by writing:void *vptr;A void pointer is sort of a generic pointer. For example, while C will not permit the comparison of a pointer to type integer with a pointer to type character, for example, either of these can be compared to a void pointer. Of course, as with other variables, casts can be used to convert from one type of pointer to another under the proper circumstances. In Program 1.1. of Chapter 1 I cast the pointers to integers into void pointers to make them compatible with the %p conversion specification. In later chapters other casts will be made for reasons defined therein.Well, that's a lot of technical stuff to digest and I don't expect a beginner to understand all of it on first reading. With time and experimentation you will want to come back and re-read the first 2 chapters. But for now, let's move on to the relationship between pointers, character arrays, and strings.CHAPTER 3: Pointers and StringsThe study of strings is useful to further tie in the relationship between pointers and arrays. It also makes it easy to illustrate how some of the standard C string functions can be implemented. Finally it illustrates how and when pointers can and should be passed to functions.In C, strings are arrays of characters. This is not necessarily true in other languages. In BASIC, Pascal, Fortran and various other languages, a string has its own data type. But in C it does not. In C a string is an array of characters terminated with a binary zero character (written as '\0'). To start off our discussion we will write some code which, while preferred for illustrative purposes, you would probably never write in an actual program. Consider, for example:char my_string[40];my_string[0] = 'T';my_string[1] = 'e';my_string[2] = 'd':my_string[3] = '\0';While one would never build a string like this, the end result is a string in that it is an array of characters terminated with a nul character. By definition, in C, a string is an array of characters terminated with the nul character. Be aware that "nul" is not the same as "NULL". The nul refers to a zero as defined by the escape sequence '\0'. That is it occupies one byte of memory. NULL, on the other hand, is the name of the macro used to initialize null pointers. NULL is #defined in a header file in your C compiler, nul may not be #defined at all.Since writing the above code would be very time consuming, C permits two alternate ways of achieving the same thing. First, one might write:char my_string[40] = {'T', 'e', 'd', '\0',};But this also takes more typing than is convenient. So, C permits:char my_string[40] = "Ted";When the double quotes are used, instead of the single quotes as was done in the previous examples, the nul character ( '\0' ) is automatically appended to the end of the string.In all of the above cases, the same thing happens. The compiler sets aside an contiguous block of memory 40 bytes long to hold characters and initialized it such that the first 4 characters are Ted\0.Now, consider the following program:------------------program 3.1-------------------------------------/* Program 3.1 from PTRTUT10.HTM 6/13/97 */#include <stdio.h>char strA[80] = "A string to be used for demonstration purposes";char strB[80];int main(void){char *pA; /* a pointer to type character */char *pB; /* another pointer to type character */puts(strA); /* show string A */pA = strA; /* point pA at string A */puts(pA); /* show what pA is pointing to */pB = strB; /* point pB at string B */putchar('\n'); /* move down one line on the screen */while(*pA != '\0') /* line A (see text) */{*pB++ = *pA++; /* line B (see text) */}*pB = '\0'; /* line C (see text) */puts(strB); /* show strB on screen */return 0;}--------- end program 3.1 -------------------------------------In the above we start out by defining two character arrays of 80 characters each. Since these are globally defined, they are initialized to all '\0's first. Then, strA has the first 42 characters initialized to the string in quotes.Now, moving into the code, we declare two character pointers and show the string on the screen. We then "point" the pointer pA at strA. That is, by means of the assignment statement we copy the address of strA[0] into our variable pA. We now use puts() to show that which is pointed to by pA on the screen. Consider here that the function prototype for puts() is:int puts(const char *s);For the moment, ignore the const. The parameter passed to puts() is a pointer, that is the value of a pointer (since all parameters in C are passed by value), and the value of a pointer is the address to which it points, or, simply, an address. Thus when we write puts(strA); as we have seen, we are passing the address of strA[0].Similarly, when we write puts(pA); we are passing the same address, since we have set pA = strA;Given that, follow the code down to the while() statement on line A. Line A states:While the character pointed to by pA (i.e. *pA) is not a nul character (i.e. the terminating '\0'), do the following:Line B states: copy the character pointed to by pA to the space pointed to by pB, then increment pA so it points to the next character and pB so it points to the next space.When we have copied the last character, pA now points to the terminating nul character and the loop ends. However, we have not copied the nul character. And, by definition a string in C must be nul terminated. So, we add the nul character with line C.It is very educational to run this program with your debugger while watching strA, strB, pA and pB and single stepping through the program. It is even more educational if instead of simply defining strB[] as has been done above, initialize it also with something like:strB[80] = "12345678901234567890123456789012345678901234567890" where the number of digits used is greater than the length of strA and then repeat the single stepping procedure while watching the above variables. Give these things a try! Getting back to the prototype for puts() for a moment, the "const" used as a parameter modifier informs the user that the function will not modify the string pointed to by s, i.e. it will treat that string as a constant.Of course, what the above program illustrates is a simple way of copying a string. After playing with the above until you have a good understanding of what is happening, we can proceed to creating our own replacement for the standard strcpy() that comes with C. It might look like:char *my_strcpy(char *destination, char *source){char *p = destination;while (*source != '\0'){*p++ = *source++;}*p = '\0';return destination;}In this case, I have followed the practice used in the standard routine of returning a pointer to the destination.Again, the function is designed to accept the values of two character pointers, i.e. addresses, and thus in the previous program we could write:。
C语言程序设计——指针(完整版)PPT教学课件

说明:
(1)设有指向整型变量的指针变量p,若要把整型变量a 的地址赋予p,有以下方式: 指针变量初始化的方法: int a; int *p=&a; 赋值语句的方法: int a,*p; p=&a; (2)指针运算符*和指针变量说明中的指针说明符*不是 一回事。后者表示其后的变量是指针类型,前者则是一 个运算符用以表示指针变量所指的变量。
三、 指针变量作函数参数 C语言是通过传值将参数传递给函数的,对被调函数来说, 没有直接的方法来改变主调函数内的变量的值。 例:对两个整数按大小顺序输出。 #include<stdio.h> swap(int x,int y) {int t; t=x; x=y; y=t; } void main( ) {int a=3,b=5; if(a<b) swap(a,b); printf(“a=%d,b=%d \n”,a,b); }
说明:
(1)指针变量名的构成原则是标识符,前面必须有 “*”, 表示该变量的类型是指针型变量。 (2)在一个定义语句中,可以同时定义普通变量、数组、 指针变量。 (3)类型说明符说明的数据类型不是指针变量中存放的 数据的数据类型,而是它将要指向的变量或数组的数据 类型。因此,一个指针变量只能用来指向同种数据类型 的其他变量或数组,不能时而指向一个浮点型变量,时 而指向一个整型变量。
说明:
(3)如果已执行了语句p=&a; ,则: &*p:先进行*p的运算,即是变量a,再执行&运算, 即变量a的地址。因此&*p与&a相同。 *&a:先进行&a的运算,即得a的地址,再执行*运 算, 即&a所指向的变量,即变量a。 因此*&a与a相同。
大学C语言实用教程(课件)-第6章-指针

int *p=&a;
// p指向a
printf("a=%p b=%p c=%p p=%p\n",&a,&b,&c,&p); printf("p=%p *p=%d\n",p,*p);
p--;
}
// p指向a前面的存储单元
printf("p=%p *p=%d\n",p,*p);
2. 指针与整数相加减 指针加减一个整数n可以使指针移动,可以访问新地址。
char c= 'A',*pc=&c; float a=3.0,*pa=&a;
指针变量指向变量示意图
pa FF74 FF70 FF71 FF72 FF73 FF74 a 3.0 FF75 FF76 FF77 FF78 pc FF7C FF79 FF7A FF7B c A FF7C
② 赋值方法 先定义指针,然后用赋值语句将所指变量的地址赋给它。例如 char c,*pc;
4. 两个同类型指针相减
x[0]
如果两个同类型指针之间所存储的数据的 x[2] 类型也与指针相同(通常是数组的情况),则相 减的结果是这两个指针之间所包含的数据个数。 x[3]
x[4]
x[1]
【例】两个同类型指针相减。 #include <stdio.h> void main() { float x[10]; float *p,*q; p=&x[2]; q=&x[8]; printf("q-p=%d\n",q-p); }
6.1.2 指针和指针变量
1.指针和指针变量的概念
变量的指针:该变量在内存中的地址。 指针变量:专门用来存放变量地址的特殊变量。 在不致引起混淆的情况下,把指针变量简称为指针。
C语言课件第9章

9.1 9.2 9.3 9.4 9.5 9.6
地址、指针和变量 指针运算 指针与数组 函数与指针 程序综合举例 上机实训
汽摩计算机学校
第9章 指 针
【本章要点】 本章主要介绍指针的基本概概念,指针运算,指针与 数组,指针与函数等内容。指针是C语言的一个重要 概念,也是C语言的一个重要特色。正确灵活的运用 指针,可以使程序简洁、紧凑、高效;可以有效地表 示复杂的数据结构。可以说,不掌握指针就没有掌握 C语言的精华。
*(p--)相当于a[i--],先取p值作“*”运算,再使p自减。 *(++p)相当于a[++i],先使p自加,再作*运算。 *(--p)相当于a[--i], 先使p自减,再作*运算。
汽摩计算机学校
第9章 指 针
【例9.5】用上面介绍的四种不同方法访问数组元素。 #include<stdio.h> void main() { int a[5]={0,1,2,3,4}; int *p,i; for(i=0;i<5;i++) 程序中的四个输出是一 { 样的。程序输出如下: printf(“%d ”,a[i]); 0 0 0 0 printf(“%d ”,*(a+i)); 1 1 1 1 printf(“%d ”,p[i]); 2 2 2 2 printf(“%d ”,*(p+i)); 3 3 3 3 printf(“\n”); 4 4 4 4 } }
汽摩计算机学校
第9章 指 针
(3)指针的关系运算 运算条件:必须是指向同一数组的2个指针变量才能进行 关系运算。 运算作用:两个指针变量通过关系运算进行比较,主要 用于判断他们的前后位置关系。 例如:设如果pa和qa都指向同一个数组a, 则有:pa>qa、pa<qa、 pa>=qa、 pa<=qa、 pa==qa、 pa!=qa 说明:当pa所指元素在qa之前,则表达式“pa<qa”为真 (值为1); 当pa和qa都指向同一个元素时,表达式“pa==qa” 为真 (值为1)。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
i_pointer=&i ;
i_pointer
...
scanf(“%d”,i_pointer);
9 . 2 Pointer variable points to a variable
The definition of pointer variable
general format:
base-type *pointer-variable ; where:base-type is the type of the value to which the pointer points
scanf ("%d%d",&a,&b);
p1=&a; p2=&b;
if (a<b)
if(*p1<*p2)
{p=p1;pl=p2;p2=p;}
printf ("\na=%d,b=%d\n”,a,b);
printf("max=%d,min=%d\n",*pl,*p2);
}
results: 5 9
a=5,b=9
main() { int a,b;
int *pointer_1, *pointer_2; a=100;b=10; pointer_1=&a; /*to assign a’s address to pointer_1*/ pointer_2=&b;/* to assign b’s address to pointer_2*/ printf (”%d,%d\n”, a,b); printf(”%d,%d\n”, *pointer_1,*pointer_2);
to a pointer)
p1
for example, int *p1,*p2, i;
p1=&i;
i
p2=p1;
p2
2. to assign a value to a variable with pointer variable
“*”:pointer operator、indirection operator is an unary operator(value-pointed-to)
to realize two variable exchange?
void swap(int p1,int p2)
{ int t;
t=p1; p1=p2; p2=t;
a
b
}
3
6
main()
{ int a,b;
printf("a,b=");
scanf("%d%d",&a,&b);
3
6
swap(a,b);
/*design a function to sort three numbers*/ void sort(int *p1,int *p2,int *p3) { if( *p1 < *p2 ) swap( p1, p2 );
Mechanism:
When system execute the called function , if the formal parameter pointer variable changes, the value is saved via actual parameter at the end of
max=9,min=5
p1
a
p
&a
5
p2
b
&b
9
p1
a
p
&b
5
p2
b
&a
9
Modify program as follows:
main() { int *p1,*p2,p, a, b;
scanf ("%d%d",&a,&b); p1=&a; p2=&b; if (*p1<*p2)
{p=*p1;*pl=*p2;*p2=p;} printf ("\na=%d,b=%d\n”,a,b); printf("max=%d,min=%d\n",*pl,*p2);
pointer variable
memory user data region
Pointer is address
...
address
1. the address of the memory storage unit ──the serial number of the storage unit
cf.data storing in the memory storage unit
pointer:The elite of C,import concept, import feature. Using pointer: 1.can make program more compact, 2.be care of using pointer
9 . 1 The concept of the pointer and
variable
address
3. The access of variable value
(1)Direct access
scanf(”%d”,&m); printf(”%d”, m);
Memory user data region
...
m=m+i;
(2) indirect access
2000 4 Variable k 2002 2 Variable m
then: &*p1 =&(*p1)=&(a)=&a=p1 *&a =*(&a)=a
(2)priority
* prior to &
3.”--”and “++”operator mostly apply to
arrays’ pointer
if :float x,*p1; p1=&x;
if p=1000,
main() { int a,b;
printf("a,b="); scanf("%d%d",&a,&b); swap(&a,&b); printf("a=%d,b=%d\n",a,b); }
&a
&b
p1
p2
Summary:
We must use pointer variable as function parameter to return the alternative value to calling function from called function
visit variable value through 2004 7 Variable i
another variable (a pointer2006 9 Variable j
variable)
.
Example:
..
define a pointer” i_pointer”
3010 2004 Variable
Chapter 9 Pointer(指针)
9.1 The conception of the pointer and pointer variable
9.2 Pointer variable points to a variable 9.3 Pointer points to an array 9.4 Pointer points to a string 9.5 Function that return a pointer 9.6 Pointer arrays and the formal parameters of the main () 9.7 Pointer points to a function
} The results:
100, 10
pointer1
&a
a(*pointer1)
100
100, 10
pointer2
b (*pointer2)
&b
10
[9.2]Input two integers a and b ,output them ordered by value .
main()
{ int *p1,*p2,*p, a, b;
for example ,int i , j , *p;
p=&i;
*p=3;
namely i=3;
j=*p+2;
After definition After p=&i
:
:
1000
i
1002
j
1004
p
1000
i
1002
j
1004 1000 p
:
:
after *p=3
after j=*p+2
1000 1002 1004
3 1000
i 1000 j 1002 p 1004
3 5 1000
i j p
Pointer variable must be assigned a value before it is
operator & and *:
(1)associativity :from right to left
if:int a, *p1; p1=&a;
Байду номын сангаас
2000 0000 0000 2001 0000 0100 2002 0000 0000 2003 0000 0111
. ..
3010 00000000 3011 11001100
...
2. Variable address and value