欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > DOC文档下载  

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

    • 资源ID:8897018       资源大小:37.50KB        全文页数:13页
    • 资源格式: DOC        下载积分:4
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录   微博登录  
    二维码
    微信扫一扫登录
    下载资源需要4
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    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 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 pointer 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 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 indicate 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, 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 efficiently. 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 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;The 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 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. 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 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 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 operator? 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 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 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 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#include<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 the X*/Printf ("Xs address is:"%xn "," P2 ");Printf (the value of X 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 the variable, that is, the address &x of the X.Or that represents 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 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 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; / * 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 comparison 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 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 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 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 ("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 parameter 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 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 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 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 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 string. 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 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<stdio.h>(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.

    注意事项

    本文(c语言指针入门(Introduction to C language pointers).doc)为本站会员(rrsccc)主动上传,三一文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




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

    三一文库
    收起
    展开