EmbLogic's Blog

Pointer->>>>>>>>>>>>>>

Introduction:->Pointer

 

A pointer is a variable that contains the address of a variable. Pointers are much used in C,

partly because they are sometimes the only way to express a computation, and partly because

they usually lead to more compact and efficient code than can be obtained in other ways.

Pointers and arrays are closely related; this chapter also explores this relationship and shows

how to exploit it.

 

This is certainly true when they are used carelessly, and it is easy to create pointers that point somewhere unexpected. With discipline, however, pointers can also be used to achieve clarity and simplicity. This is the aspect that we will try to illustrate.

 

The main change in ANSI C is to make explicit the rules about how pointers can be

manipulated, in effect mandating what good programmers already practice and good compilers

already enforce. In addition, the type void * (pointer to void) replaces char * as the proper

type for a generic pointer.

 

Pointers and Addresses:-

 

Let us begin with a simplified picture of how memory is organized. A typical machine has an

array of consecutively numbered or addressed memory cells that may be manipulated

individually or in contiguous groups. One common situation is that any byte can be a char, 4 byte cells can be treated as a short integer, and 8 adjacent bytes form a long. A

pointer is a group of cells (often two or four) that can hold an address.

So if c is a char and p is a pointer that points to it, we could represent the situation this way:

 

The unary operator & gives the address of an object, so the statement

 

p = &c;

 

assigns the address of c to the variable p, and p is said to “point to” c. The & operator only

applies to objects in memory: variables and array elements. It cannot be applied to expressions,

constants, or register variables.

The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it

accesses the object the pointer points to. Suppose that x and y are integers and ip is a pointer

to int. This artificial sequence shows how to declare a pointer and how to use & and *:

int x = 1, y = 2, z[10];

int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */

y = *ip; /* y is now 1 */

*ip = 0; /* x is now 0 */

ip = &z[0]; /* ip now points to z[0] */

The declaration of x, y, and z are what we’ve seen all along.

 

Pointers and Arrays:-

 

In C, there is a strong relationship between pointers and arrays, strong enough that pointers

and arrays should be discussed simultaneously. Any operation that can be achieved by array

subscripting can also be done with pointers. The pointer version will in general be faster but, at

least to the uninitiated, somewhat harder to understand.

The declaration

int a[10];

defines an array of size 10, that is, a block of 10 consecutive objects named a[0], a[1],

…,a[9].

 

The notation a[i] refers to the i-th element of the array. If pa is a pointer to an integer,

declared as

int *pa;

then the assignment

pa = &a[0];

sets pa to point to element zero of a; that is, pa contains the address of a[0].

Now the assignment

x = *pa;

will copy the contents of a[0] into x.

If pa points to a particular element of an array, then by definition pa+1 points to the next

element, pa+i points i elements after pa, and pa-i points i elements before. Thus, if pa points

to a[0],

*(pa+1)

refers to the contents of a[1], pa+i is the address of a[i], and *(pa+i) is the contents of

a[i].

 

These remarks are true regardless of the type or size of the variables in the array a. The

meaning of “adding 1 to a pointer,” and by extension, all pointer arithmetic, is that pa+1 points

to the next object, and pa+i points to the i-th object beyond pa.

The correspondence between indexing and pointer arithmetic is very close. By definition, the

value of a variable or expression of type array is the address of element zero of the array. Thus

after the assignment

pa = &a[0];

pa and a have identical values. Since the name of an array is a synonym for the location of the

initial element, the assignment pa=&a[0] can also be written as

pa = a;

Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as

*(a+i). In evaluating a[i], C converts it to *(a+i) immediately; the two forms are

equivalent. Applying the operator & to both parts of this equivalence, it follows that &a[i] and

a+i are also identical: a+i is the address of the i-th element beyond a. As the other side of this

coin, if pa is a pointer, expressions might use it with a subscript; pa[i] is identical to *(pa+i).

In short, an array-and-index expression is equivalent to one written as a pointer and offset.

There is one difference between an array name and a pointer that must be kept in mind. A

pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable;

constructions like a=pa and a++ are illegal.

When an array name is passed to a function, what is passed is the location of the initial

element. Within the called function, this argument is a local variable, and so an array name

parameter is a pointer, that is, a variable containing an address. We can use this fact to write

another version of strlen, which computes the length of a string.

/* strlen: return length of string s */

int strlen(char *s)

{

int n;

for (n = 0; *s != ”, s++)

n++;

return n;

}

Since s is a pointer, incrementing it is perfectly legal; s++ has no effect on the character string

in the function that called strlen, but merely increments strlen’s private copy of the pointer.

That means that calls like

strlen(“hello, world”); /* string constant */

strlen(array); /* char array[100]; */

strlen(ptr); /* char *ptr; */

all work.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>