首页 > 科技 > Pointer

Pointer

2007年8月23日 14点01分 发表评论 阅读评论

The duties of the pointer were to point out, by calling their names, those in the congregation who should take note of some point made in the sermon.

What is a pointer?

A pointer is a variable whose value is used to point to another variable. From the definition, you know two things: first, that a pointer is a variable, so you can assign different values to a pointer and second, that the values contained by a pointer must be an address that indicates the location of another variable in the memory. That’s why a pointer is also called an address variable.

Address(Left Value) versus Contents(Right Value)
Each memory location must have a unique address so that the computer can read from or write to the memory location without any confusion. This is similar to the concept that each house in a city must have a unique address. Right value, can be thought as a letter deliveved to the mailbox.

Note, when your C program is being compiled, and a value is being assigned to a variable, the C compiler has to check the left value of the variable. If the compiler cannot find the left value, if will issue an error message saying that the variable is undefined in your program. That’s why in C, you have to declare a variable before you can use it.

The Address-of operator: &
Return the address(that is, the left value) of a variable.
eg: long int x,y;
y = &x;
assignes the address of the x to the address variable y.

The format specifier %p is used in the printf() funtion for displaying the address.(Hexadecimal Format)

Declare Pointers
A pointer is a variable, which means that a pointer has a left value and right value as well. However, both the left and right values are addresses. The left value of a pointer is used to refer to the pointer itself, whereas the right value of a pointer, which is the content of the pointer, is the address of another variable.

The general form of a pointer declaration:
data-type *pointer-name
data-type specifies the type of data to which the pointer points.
pointer-name is the name of the pointer variable.
asterisk indicates the variable is a pointer, is called dereference operator(indirection operator).

Null pointer, a pointer is said to be a null pointer when its right value is 0. Remember, a null pointer can never point to valid data. Simply assign 0 to the pointer value. eg: ptr_c = 0

Pointing to the same thing: a memory location can be pointed by more than one pointer.
eg: ptr_c1 = &c; ptr_c2 = &c;
Point to the same location in the memory.

Pointer and Guidepost:
Memory — City
Pointer — Guidepost
Pointer variable Left Value — The position of Guidepost in the city
Pointer variable Right Value — The text on the Guidepost, which is somebody’s home address
Null Pointer: Set Pointer variable Right Value as 0, means no content on the Guidepost, as empty.
Multiple Pointers: Many Guideposts are mapping to the same home address.
*Pointer — People who lives in the house that is indicated on the Guidepost.
data-type *Pointer — What’s kind of people?

分类: 科技 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
您必须在 登录 后才能发布评论.