c语言指针入门(Introduction to C language pointers).doc

上传人:rrsccc 文档编号:8897018 上传时间:2021-01-23 格式:DOC 页数:13 大小:37.50KB
返回 下载 相关 举报
c语言指针入门(Introduction to C language pointers).doc_第1页
第1页 / 共13页
c语言指针入门(Introduction to C language pointers).doc_第2页
第2页 / 共13页
c语言指针入门(Introduction to C language pointers).doc_第3页
第3页 / 共13页
c语言指针入门(Introduction to C language pointers).doc_第4页
第4页 / 共13页
c语言指针入门(Introduction to C language pointers).doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《c语言指针入门(Introduction to C language pointers).doc》由会员分享,可在线阅读,更多相关《c语言指针入门(Introduction to C language pointers).doc(13页珍藏版)》请在三一文库上搜索。

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

31、 of man, for example:Printf (man); calling this library function gives the man argument, which is also the address of the first character M.A string can be represented in three ways, and they can all be considered as pins: for example, char *ch= man; a character is createdPointer ch; and assign a st

32、ring. In fact, ch takes up only two bytes of memory. That is the first address of the string (the address of M),The string itself contains the data segment of memory. Static char ch4= man; defines a character array ch,And the initial value is given. Man string constant.These three methods can all be

33、 regarded as pointers. The second method uses static storage methods to assign initial values, but the memory overhead is large,Occupies four bytes of memory space. The first approach saves memory and runs faster.Example: print the man string in the three ways above.F10-3.c#include(main)Char*ch= man;Static char s4= man;Printf (this%s:%s:%s string. n, ch, s, man ;Display after program execution:This man:man:man string.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1