c语言指针入门IntroductiontoClanguagepointers_第1页
c语言指针入门IntroductiontoClanguagepointers_第2页
c语言指针入门IntroductiontoClanguagepointers_第3页
c语言指针入门IntroductiontoClanguagepointers_第4页
c语言指针入门IntroductiontoClanguagepointers_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c 语言指针入门( Introduction to C language pointers)Pointer C programming language entry (thirteen) Li Changlin, head office of soft CorporationUsing 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 maybe difficultfor beginners to understand,and the library below is an example of what a pointer

3、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.This p

4、rocess 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 indic

5、ate 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 pointers,

6、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 efficientl

7、y. 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 exte

8、nded 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;The c

9、h 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 an

10、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 manymore complicated definition examples of pointer, which will be explained in detail later. The

11、reading of the pointer is:Read the nameof the pointer first, read the nameto 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 before

12、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 itrefers to. For example, int *n; two bytes on a general

13、 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 & aren't they by bitwise and operator?What is the address oper

14、ator? 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 pointer's positionWhen a variable is called, it indicates that this va

15、riable is a pointer. We can see this is a duplicate definition (and sign (*) is repeated). butWhenused, 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 in

16、itial 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);=5Display results: N=5, P points to the value of the variable The above four statements illustr

17、ate the basic 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#inc

18、lude<stdio.h>(main)Int 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 theX*/Printf ("X's address is:"%xn "," P2 ");Printf (the value of X

19、is:%dn, *P1);Display of results after program execution:The X address 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

20、the variable, that is, the address &x of the X.Or that represents the pointer variable. The value of the P1itself 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 int

21、eger variable x.3. before you makethe * operator, you should assign the 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

22、 an integer variable length.Ch+; / * character pointer moves a character 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 operatio

23、ns. Can also do other ten, one integer operation. Such as:I=i+6; / * pointer 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 comparis

24、on of the two pointers is to compare their address values, that is, the 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

25、you can say this: the array name meansPointer to the first element of an array.Example: char ch81;Char *p;P=ch; / * the array is assigned to the first address pointerP*/If you access tenth elements in an array, write this way:Ch9 or *P+9/* array elements from scratch / C language provides two ways t

26、o access array elements,One is pointer operation; two is array subscript 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<stdio.h>(main)Charch30;Int (STR); / * statement functionIntPrintf (&

27、quot;enter characters within 30 and enter" n ");Scanf ("%s", CH);Printf ("this%s string", "ch");A=str (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 par

28、ameter descriptionInt n; n=0;While (*s) / * pointer value is nonzero if true * / cycleS+; a pointer position.N+; /*n count a * /Retrun n; / * 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 func

29、tion is defined, the parameter is saidMingcheng pointer to char, so the s pointer is ch array address received s pointer points to an array 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

30、, *s means that the value referred to by the pointer is true (zero). That's SIndex set,As long as characters have been circulating.S+is the pointerthat 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

31、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 example:Printf ("man"); calling this library function gives the "man" argument, which is also the address of

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论