学习使用指针 UNDERSTANDING POINTERS (for beginners)

                    学习使用指针

           UNDERSTANDING POINTERS (for beginners)

                      by  Ted Jensen

                       Version 0.0

      This material is hereby placed in the public domain.

                     September 5, 1993


                     TABLE OF CONTENTS


    INTRODUCTION;


    CHAPTER 1: What is a pointer?


    CHAPTER 2: Pointer types and Arrays


    CHAPTER 3: Pointers and Strings


    CHAPTER 4: More on Strings


    CHAPTER 5: Pointers and Structures


    CHAPTER 6: Some more on Strings, and Arrays of Strings


    EPILOG:


==================================================================


INTRODUCTION:


    Over a period of several years of monitoring various

telecommunication conferences on C I have noticed that one of the

most difficult problems for beginners was the understanding of

pointers.  After writing dozens of short messages in attempts to

clear up various fuzzy aspects of dealing with pointers, I set up

a series of messages arranged in "chapters" which I could draw

from or email to various individuals who appeared to need help in

this area.


    Recently, I posted all of this material in the FidoNet CECHO

conference.  It received such a good acceptance, I decided to

clean it up a little and submit it for inclusion in Bob Stout's

SNIPPETS file.


    It is my hope that I can find the time to expand on this text

in the future.  To that end, I am hoping that those who read this

and find where it is lacking, or in error, or unclear, would

notify me of same so the next version, should there be one, I can

correct these deficiencys.


    It is impossible to acknowledge all those whose messages on

pointers in various nets contributed to my knowledge in this

area.  So, I will just say Thanks to All.


    I frequent the CECHO on FidoNet via RBBSNet and can be

contacted via the echo itself or by email at:


     RBBSNet address 8:916/1.


I can also be reached via


Internet email at ted.jensen@spacebbs.com


Or     Ted Jensen

       P.O. Box 324

       Redwood City, CA 94064


==================================================================

CHAPTER 1: What is a pointer?


    One of the things beginners in C find most difficult to

understand is the concept of pointers.  The purpose of this

document 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 (on a PC) of memory to hold the value of the

integer.  It also sets up a symbol table. And in that table it

adds the symbol k and the address in memory where those 2 bytes

were set aside.


    Thus, later if we write:


    k = 2;


at run time we expect that the value 2 will be placed in that

memory location reserved for the storage of the value of k.


    In a sense there are two "values" associated with k, one

being the value of the integer stored there (2 in the above

example) and the other being the "value" of the memory location

where it is stored, 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, pronunced "el

value") respectively.


    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 statment, the '2' above.

Note that rvalues cannot be used on the left side of the

assignment statement.  Thus:    2 = k;   is illegal.


    Okay, now consider:


    int j, k;

    k = 2;

    j = 7;    <-- line 1

    k = j;    <-- line 2


    In 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 will hopefully 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

definition:


    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 pointer variable to store the address of an

integer. Such a pointer is said to "point to" an integer.  Note,

however, that when we wrote  "int k;" we did not give k a value.

If this definiton was made outside of any function many compilers

will initialize it to zero.  Simlarly, ptr has no value, that is

we haven't stored an address in it in the above definition.  In

this case, again if the definition is outside of any function, it

is intialized to a value #defined by your compiler as NULL.  It

is called a NULL pointer.  While in most cases NULL is #defined

as zero, it need not be.  That is, different compilers handle

this differently.  Also note that while zero is an integer, NULL

need not be.


    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 refering to the value of that which ptr is pointing

at, 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.


-------------------------------------------------

#include


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,&j);

   printf("k has the value %d and is stored at %p/n",k,&k);

   printf("ptr has the value %p and is stored at %p/n",ptr,&ptr);

   printf("The value of the integer pointed to by ptr is %d/n",

           *ptr);

   return 0;

}

---------------------------------------

To review:


    A variable is defined by giving it a type and a name (e.g.

     int k;)


    A pointer variable is defined 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 this case).


    Once a variable is defined, 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值