版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、c语言指针入门(Introduction to C language pointers)Pointer C programming language entry (thirteen)- Li Changlin, head office of soft Corporation-Using pointers is one of the most important features of C language. What is a pointer? A pointer is a variable class that holds the address of memoryType. That is
2、 to say, the pointer is used to specify the location of a variable in memory. Or, the pointer is pointing to anotherA variable. The pointer and the address are closely linked together.This shows that it may be difficult for beginners to understand, and the library below is an example of what a point
3、er is.In the library, the library in every book there is a whole stack of press ISBN, ISBN aligned. CodesCan be regarded as the address of the book in the library. Each book also has an index card to find a book, first from an index cardTo find this book ISBN, then press ISBN book in the library. Th
4、is process instruction number indicates the location of the book in the librarySet, that is, the address. The book is an index card pointer variable, on top of it is the memory address book number. And that numberThe book on the location of the code is the variable it refers to. Thus, you can also i
5、ndicate the pointer: index card (pointer). This variable isTo indicate the location of a Book (a variable) in a stack (in memory); or index cards (pointers) that point to a BookA variable (another variable).Pointers manipulate variables through the indirect address of the memory address. With pointe
6、rs, this data type can be more complex to generateData structures, such as linked lists, two tree, etc., can make some complex data, such as array, structure, union and other dataThe transfer and operation of functions becomes easy, and the compiled target code can be executed faster and more effici
7、ently. becauseThis study C language, the concept of pointer must understand, correct use.First, the definition of the pointer, form, type identifier * pointer name;Type identifier: the data type of the object to which the pointer is specified. For data types, it can be basic dataType can also be an
8、extended data type.*: represents the pointer operator. It has two functions: first, to define pointer variables, and two to specify the values of the variables referred to by the pointer.Pointer Name: Specifies the user specified name identifier in accordance with the C language.Example 1:Char *ch;T
9、he ch pointer is defined, which refers to the character type variable.Example 2:int *n;The N pointer is defined, which refers to integer variables.Example 3:float *5 pointer, which refers to a single precision floating point variable.Example 4:int (*p) 10;The P pointer is defined. It is a pointer to
10、 an array. There are 10 elements in the array, each of which is integer.Example 5: int *p10;Defines an array of P, with 10 elements in the array, each of which is a pointer to an integer variable.There are many more complicated definition examples of pointer, which will be explained in detail later.
11、 The reading of the pointer is:Read the name of the pointer first, read the name to the right, and then read the name of the pointer to the left. If you need to read the pointer first, read the left side and the right sideWith parentheses. Example 4.In addition, the keyword far or near may be added
12、before the type identifier and the * number to indicate the distant pointer or the near pointer.Example int far *n;This example defines a far pointer to N, it is pointing to an integer variable.The length of a pointer is dependent on the type of data it refers to. For example, int *n; two bytes on a
13、 general system. And intfar *n; four bytes.Two, pointer operationWhen the pointer is operated, first, the following two operators are shown as follows:- - the address operator, which returns the address of operands. You may ask questions & arent they by bitwise and operator?What is the address opera
14、tor? Yes, the C language allows you to define an operator repeatedly, and never confuse it when using it.* pointer operator, also referred to as indirection operator. In operation, it returns the value of the variable in the pointers positionWhen a variable is called, it indicates that this variable
15、 is a pointer. We can see this is a duplicate definition (and sign (*) is repeated). butWhen used, it is not confused because of the different positions in the program. C language definition and repeat operator, such as minus (-) and minus (-).Such as:Int n=5;* defines the integer variable n initial
16、 value 5*/int *p; / * P defines a pointer, which points to integer data */p=&n;* the variable n address assigned to the p*/printf pointer (N=%d, P points to the value of the variable =%dn, N, *p);Display results: N=5, P points to the value of the variable =5The above four statements illustrate the b
17、asic use of pointers: first, define pointer variables, before you use themThe pointer points to the variable to be operated. That is, the address of the variable to which the pointer is pointing.Example: p=&n;Thus, you can use this pointer in the following statement.Example: f10-1.c#include(main)Int
18、 x=5;Int *p1, *p2; / * define two pointer * /P1=&x; / * pointer to P1 Fu address, that point to the variable X*/P2=P1; / * P1 address assigned to P2, which is the same to the X*/Printf (Xs address is:%xn , P2 );Printf (the value of X is:%dn, *P1);Display of results after program execution:The X addr
19、ess is: f5e /x system address different changes.The value of X is: 5For the f10-1 and C programs, here are three points:1. a pointer that does not have an asterisk in front of the address. For example, the P1 indicates the address to the variable, that is, the address &x of the X.Or that represents
20、the pointer variable. The value of the P1 itself is the address of X. Such as P1=&x;2., a pointer, which indicates the value of the variable referred to, should be preceded by an asterisk, such as *P1, like the value of the integer variable x.3. before you make the * operator, you should assign the
21、address to the pointer.Three. Arithmetic operation of pointerArithmetic operations on pointers are only ten and one.For example: int*i; / * define a pointer to i*/Char*ch / / defines a character pointer cb*/I+; / * pointer moves an integer variable length.Ch+; / * character pointer moves a character
22、 variable length.A character pointer is one word per move, and an integer pointer is one word per move.That is, the length of the data type they refer to.Pointers can be used not only for incremental but also descending operations. Can also do other ten, one integer operation. Such as:I=i+6; / * poi
23、nter back 6 element length.I=i-3; / * pointer to move forward 3 elements of the length.Ch=ch+3; / * pointer back 3 element length.Ch=ch-2; / * pointer to move forward 2 elements of the length.Four, pointer comparisonThe comparison of the two pointers is to compare their address values, that is, the
24、addresses of the variables they refer to. Such as:Int, *tos, *p1;If (p1=tos)? if (p1= (tos+50)Five pointers and arraysIn the array statement, it was said that the array name is the address of the first element of the array. Now you can say this: the array name meansPointer to the first element of an
25、 array.Example: char ch81;Char *p;P=ch; / * the array is assigned to the first address pointer P*/If you access tenth elements in an array, write this way:Ch9 or *P+9/* array elements from scratch / C language provides two ways to access array elements,One is pointer operation; two is array subscrip
26、t operation. Because pointers are so fast, they usually use pointers to access array elements.Example: enter a string, and count the numberF10-2:c#include(main)Charch30;Int (STR); / * statement functionIntPrintf (enter characters within 30 and enter n );Scanf (%s, CH);Printf (this%s string, ch);A=st
27、r (CH); / *; and to call the function arguments as an array of first address.Printf (a total of%d characters n, a);Int:str (char*s) / * * / pointer into the parameter descriptionInt n; n=0;While (*s) / * pointer value is nonzero if true * / cycleS+; a pointer position.N+; /*n count a * /Retrun n; /
28、* returns the number of characters.This program gives the first address of the array element in str (CH); the argument given by the function call; when the function is defined, the parameter is saidMingcheng pointer to char, so the s pointer is ch array address received s pointer points to an array
29、of Ch. SeeIs a value passing call, but a reference call. The address of the array is passed, not the entire array.In the str () function, in the while (*s) loop, *s means that the value referred to by the pointer is true (zero). Thats SIndex set,As long as characters have been circulating. S+ is the
30、 pointer that moves from the first element of the array, moves one character at a time, and n+ counts at a time.Six, pointer and stringA string of characters enclosed in double quotes, such as man, called string constants. The pointer to a string constant is its first wordYes, M is the first address of man, for exampl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度印刷厂与出版社合作打印合同范本4篇
- 2025年度外墙保温技术改造项目施工合同书3篇
- 2025年度生态旅游开发承包合同模板4篇
- 2024舞蹈赛事组织与管理服务合同
- 2025年度特色小吃店联合经营合同3篇
- 2025年度厨房设备安装与用户培训支持合同3篇
- 2025年度物流中心承包经营合作协议书4篇
- 2024退学协议书:涉及在线教育平台学员退费及课程重置合同3篇
- 2024网络安全防护系统技术开发与服务合同
- 2024版设备软件采购及技术服务合同
- 上海车位交易指南(2024版)
- 医学脂质的构成功能及分析专题课件
- 通用电子嘉宾礼薄
- 钱素云先进事迹学习心得体会
- 道路客运车辆安全检查表
- 宋晓峰辣目洋子小品《来啦老妹儿》剧本台词手稿
- 附录C(资料性)消防安全评估记录表示例
- 噪音检测记录表
- 推荐系统之协同过滤算法
- 提高筒仓滑模施工混凝土外观质量QC成果PPT
- 小学期末班级颁奖典礼动态课件PPT
评论
0/150
提交评论