一本介绍C指针的书--指针是什么1.2


However, at this point, the definition originally cited above is sufficient. As we become

more familiar with pointers we will go into more detail on this. 

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 hopefully will 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 declaration: 

   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.

However, note that when we wrote int k; we did not give k a value. If this definition is

made outside of any function ANSI compliant compilers will initialize it to zero.

Similarly, ptr has no value, that is we haven't stored an address in it in the above

declaration. In this case, again if the declaration is outside of any function, it is initialized

to a value guaranteed in such a way that it is guaranteed to not point to any C object or

function. A pointer initialized in this manner is called a "null" pointer. 

The actual bit pattern used for a null pointer may or may not evaluate to zero since it

depends on the specific system on which the code is developed. To make the source code

compatible between various compilers on various systems, a macro is used to represent a

null pointer. That macro goes under the name NULL. Thus, setting the value of a pointer

using the NULL macro, as with an assignment statement such as ptr = NULL, guarantees

that the pointer has become a null pointer. Similarly, just as one can test for an integer

value of zero, as in if(k == 0), we can test for a null pointer using if (ptr == NULL). 

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

------------ Program 1.1 --------------------------------- 

 

/* Program 1.1 from PTRTUT10.TXT   6/10/97 */

#include <stdio.h>

 

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

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

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

    printf("The value of the integer pointed to by ptr is %d\n", *ptr);

 

    return 0;

}

Note: We have yet to discuss those aspects of C which require the use of the (void *)

expression used here. For now, include it in your test code. We'll explain the reason

behind this expression later. 

转载于:https://my.oschina.net/clearchen/blog/70812

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值