Sunteți pe pagina 1din 28

http://www.authorstream.com/Presentation/aSGuest8537-129547-ethics-presentation-spiritualinspirational-ppt-powerpoint/ I.

DEREFERENCING POINTER

1.The dereference operator or indirection operator, denoted by "*" (i.e. an asterisk), is a unary operator found in C-like languages that include pointer variables. It operates on a pointer variable, and returns an l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer. For example, the C code
int x = 0; int *pointer_to_x = &x; (*pointer_to_x) += 1; //x is now equal to 1

increments the variable x by using the indirection operator and a pointer to the variable x. 2.

Pointer Basics

This document introduces the basics of pointers as they work in several computer languages -- C, C++, Java, and Pascal. This document is the companion document for the Pointer Fun with Binky digital video, or it may be used by itself.

Section 1 -- The three basic rules of pointers Section 2 -- A simple code example (the same example used in the video) Section 3 -- Study questions with solutions

This is document 106 in the Stanford CS Education Library. This and other free materials are available at cslibrary.stanford.edu. Some documents that are related to this one include...

Pointer Fun Video -- a silly 3 minute digital video on the basics of pointers. Designed to go with the document in front of you. (http://cslibrary.stanford.edu/104/) Pointers and Memory --a 31 page explanation of the common features and techniques for using pointers and memory in C and other languages. (http://cslibrary.stanford.edu/102/)

Section 1 -- Pointer Rules


One of the nice things about pointers is that the rules which govern how they work are pretty simple. The rules can be layered together to get complex results, but the individual rules remain simple.

1) Pointers and Pointees


A pointer stores a reference to something. Unfortunately there is no fixed term for the thing that the pointer points to, and across different computer languages there is a wide variety of things

that pointers point to. We use the term pointee for the thing that the pointer points to, and we stick to the basic properties of the pointer/pointee relationship which are true in all languages. The term "reference" means pretty much the same thing as "pointer" -- "reference" implies a more high-level discussion, while "pointer" implies the traditional compiled language implementation of pointers as addresses. For the basic pointer/pointee rules covered here, the terms are effectively equivalent.

The above drawing shows a pointer named x pointing to a pointee which is storing the value 42. A pointer is usually drawn as a box, and the reference it stores is drawn as an arrow starting in the box and leading to its pointee. Allocating a pointer and allocating a pointee for it to point to are two separate steps. You can think of the pointer/pointee structure as operating at two levels. Both the levels must be set up for things to work. The most common error is concentrating on writing code which manipulates the pointer level, but forgetting to set up the pointee level. Sometimes pointer operations that do not touch the pointees are called "shallow" while operations on the pointees are called "deep".

2) Dereferencing
The dereference operation starts at the pointer and follows its arrow over to access its pointee. The goal may be to look at the pointee state or to change the pointee state. The dereference operation on a pointer only works if the pointer has a pointee -- the pointee must be allocated and the pointer must be set to point to it. The most common error in pointer code is forgetting to set up the pointee. The most common runtime crash because of that error in the code is a failed dereference operation. In Java the incorrect dereference will be flagged politely by the runtime system. In compiled languages such as C, C++, and Pascal, the incorrect dereference will sometimes crash, and other times corrupt memory in some subtle, random way. Pointer bugs in compiled languages can be difficult to track down for this reason.

3) Pointer Assignment
Pointer assignment between two pointers makes them point to the same pointee. So the assignment y = x; makes y point to the same pointee as x. Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer. After pointer assignment, the two pointers are said to be "sharing" the pointee.

Section 2 -- Binky's Code Example


This section presents the same code example used in the Pointer Fun With Binky video. There are versions of the code in several computer languages. All the versions have the same structure and demonstrate the same basic rules and lessons about pointers; they just vary in their syntax. Independent of any particular language, the basic structure of the example is...

1. Allocate two pointers x and y. Allocating the pointers does not allocate any pointees. 2. Allocate a pointee and set x to point to it. Each language has its own syntax for this. What matters is that memory is dynamically allocated for one pointee, and x is set to point to that pointee. 3. Dereference x to store 42 in its pointee. This is a basic example of the dereference operation. Start at x, follow the arrow over to access its pointee. 4. Try to dereference y to store 13 in its pointee. This crashes because y does not have a pointee -it was never assigned one. 5. Assign y = x; so that y points to x's pointee. Now x and y point to the same pointee -- they are "sharing". 6. Try to dereference y to store 13 in its pointee. This time it works, because the previous assignment gave y a pointee.

C Version
The pointers x and y are allocated as local variables. The type int* means "pointer which points to ints". As Binky learns, the pointers do not automatically get pointees. The pointee for x is dynamically allocated separately with the standard library function malloc(). The syntax *x dereferences x to access its pointee.
void main() { int* x; int* y; // Allocate the pointers x and y // (but not the pointees) // Allocate an int pointee, // and set x to point to it

x = malloc(sizeof(int)); *x = 42; *y = 13; y = x;

// Dereference x to store 42 in its pointee // CRASH -- y does not have a pointee yet // Pointer assignment sets y to point to x's pointee

*y = 13; }

// Dereference y to store 13 in its (shared) pointee

Another way to play with pointers in C (or C++) is using the ampersand (&) operator to compute a pointer to local memory in the stack. However, pointees dynamically allocated in the heap are the most common, so that's what we show.

Void pointers
1.At first glance, a void pointer seems to be of limited, if any, use. However, when combined with the ability to cast such a pointer to another type, they turn out to be quite useful and flexible. Consider the example of the previous section, where we constructed a function pointer to a function of type void and argument int. Such a function pointer in this form could not be used for a void function with a different type of argument (for example, float). This can be done, however, through the use of void pointers, as the following example illustrates.
#include <stdio.h> void use_int(void *); void use_float(void *); void greeting(void (*)(void *), void *); int main(void) { char ans; int i_age = 22; float f_age = 22.0; void *p; printf("Use int (i) or float (f)? "); scanf("%c", &ans); if (ans == 'i') { p = &i_age; greeting(use_int, p); } else { p = &f_age; greeting(use_float, p); } return 0; } void greeting(void (*fp)(void *), void *q) { fp(q); } void use_int(void *r) { int a; a = * (int *) r; printf("As an integer, you are %d years old.\n", a); } void use_float(void *s) { float *b; b = (float *) s; printf("As a float, you are %f years old.\n", *b);

Although this requires us to cast the void pointer into the appropriate type in the relevant subroutine (use_int or use_float), the flexibility here appears in the greeting routine, which can now handle in principle a function with any type of argument. This will especially become apparent when we discuss structures in the next section. 2.

Null pointer

A null pointer has a value reserved for indicating that the pointer does not refer to a valid object. Null pointers are routinely used to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type. Null pointers are often considered similar to null values in relational databases, but they have somewhat different semantics. Null pointer in most programming languages means "no value", while null value in relational database means "unknown value". This leads to important difference in practice: two null pointers are considered equal in most programming languages, but two null values in relational database are not (since they represent unknown values, it is unknown whether they are equal). In some programming language environments (at least one proprietary Lisp implementation, for example),[citation needed] the value used as the null pointer (called nil in Lisp) may actually be a pointer to a block of internal data useful to the implementation (but not explicitly reachable from user programs), thus allowing the same register to be used as a useful constant and a quick way of accessing implementation internals. This is known as the nil vector. In C, two null pointers of any type are guaranteed to compare equal.[7] The macro NULL is defined as an implementation-defined null pointer constant,[3] which in C99 can be portably expressed as the integer value 0 converted implicitly or explicitly to the type void*.[8] Note, though, that the physical address zero is often directly accessible by hardware (for instance, it is directly accessible in x86 real mode, and it is where the interrupt table is stored), but modern operating systems usually map virtual address spaces in such a way that accessing address zero is forbidden. In C++, while the NULL macro was inherited from C, the integer literal for zero has been traditionally preferred to represent a null pointer constant.[9] However, C++11 has introduced an explicit nullptr constant to be used instead. A null pointer should not be confused with an uninitialized pointer: A null pointer is guaranteed to compare unequal to any pointer that points to a valid object. However, depending on the language and implementation, an uninitialized pointer has either an indeterminate (random or meaningless) value or a specific value that is not necessarily any kind of null pointer constant. The null reference was invented by C.A.R. Hoare in 1965 as part of the Algol W language. Hoare later (2009) described his invention as a "billion-dollar mistake":[10][11]

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. Because a null pointer does not point to a meaningful object, an attempt to dereference a null pointer usually causes a run-time error:

In C, the behavior of dereferencing a null pointer is undefined.[12] Many implementations cause such code to result in the program being halted with a segmentation fault, because the null pointer representation is chosen to be an address that is never allocated by the system for storing objects. However, this behavior is not universal. In Java, access to a null reference triggers a NullPointerException, which can be caught by error handling code, but the preferred practice is to ensure that such exceptions never occur. In Objective-C, messages may be sent to a nil object (which is essentially a null pointer) without causing the program to be interrupted; the message is simply ignored, and the return value (if any) is nil or 0, depending on the type.[13]

In languages with a tagged architecture, a possibly-null pointer can be replaced with a tagged union which enforces explicit handling of the exceptional case; in fact, a possibly-null pointer can be seen as a tagged pointer with a computed tag. II.pointer to pointer 1. Since we can have pointers to int, and pointers to char, and pointers to any structures we've defined, and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers. If we're used to thinking about simple pointers, and to keeping clear in our minds the distinction between the pointer itself and what it points to, we should be able to think about pointers to pointers, too, although we'll now have to distinguish between the pointer, what it points to, and what the pointer that it points to points to. (And, of course, we might also end up with pointers to pointers to pointers, or pointers to pointers to pointers to pointers, although these rapidly become too esoteric to have any practical use.) The declaration of a pointer-to-pointer looks like
int **ipp;

where the two asterisks indicate that two levels of pointers are involved. Starting off with the familiar, uninspiring, kindergarten-style examples, we can demonstrate the use of ipp by declaring some pointers for it to point to and some ints for those pointers to point to:
int i = 5, j = 6; k = 7;

int *ip1 = &i, *ip2 = &j;

Now we can set


ipp = &ip1;

and ipp points to ip1 which points to i. *ipp is ip1, and **ipp is i, or 5. We can illustrate the situation, with our familiar box-and-arrow notation, like this:

If we say
*ipp = ip2;

we've changed the pointer pointed to by ipp (that is, ip1) to contain a copy of ip2, so that it (ip1) now points at j:

If we say
*ipp = &k;

we've changed the pointer pointed to by ipp (that is, ip1 again) to point to k:

What are pointers to pointers good for, in practice? One use is returning pointers from functions, via pointer arguments rather than as the formal return value. To explain this, let's first step back and consider the case of returning a simple type, such as int, from a function via a pointer argument. If we write the function
f(int *ip) { *ip = 5; }

and then call it like this:


int i; f(&i);

then f will ``return'' the value 5 by writing it to the location specified by the pointer passed by the caller; in this case, to the caller's variable i. A function might ``return'' values in this way if it had multiple things to return, since a function can only have one formal return value (that is, it can only return one value via the return statement.) The important thing to notice is that for the function to return a value of type int, it used a parameter of type pointer-to-int.

Now, suppose that a function wants to return a pointer in this way. The corresponding parameter will then have to be a pointer to a pointer. For example, here is a little function which tries to allocate memory for a string of length n, and which returns zero (``false'') if it fails and 1 (nonzero, or ``true'') if it succeeds, returning the actual pointer to the allocated memory via a pointer:
#include <stdlib.h> int allocstr(int len, char **retptr) { char *p = malloc(len + 1); if(p == NULL) return 0; *retptr = p; return 1; }

/* +1 for \0 */

The caller can then do something like


char *string = "Hello, world!"; char *copystr; if(allocstr(strlen(string), &copystr)) strcpy(copystr, string); else fprintf(stderr, "out of memory\n"); (This is a fairly crude example; the allocstr function is not terribly useful. It would have been just about as easy for the caller to call malloc directly. A different, and more useful, approach to writing a ``wrapper'' function around malloc is exemplified by the chkmalloc function we've

been using.) One side point about pointers to pointers and memory allocation: although the void * type, as returned by malloc, is a ``generic pointer,'' suitable for assigning to or from pointers of any type, the hypothetical type void ** is not a ``generic pointer to pointer.'' Our allocstr example can only be used for allocating pointers to char. It would not be possible to use a function which returned generic pointers indirectly via a void ** pointer, because when you tried to use it, for example by declaring and calling
double *dptr; if(!hypotheticalwrapperfunc(100, sizeof(double), &dptr)) fprintf(stderr, "out of memory\n"); would not be passing a void **, but rather a double **.

you

Another good use for pointers to pointers is in dynamically allocated, simulated multidimensional arrays, which we'll discuss in the next chapter. As a final example, let's look at how pointers to pointers can be used to eliminate a nuisance we've had when trying to insert and delete items in linked lists. For simplicity, we'll consider lists of integers, built using this structure:
struct list { int item; struct list *next;

};

Suppose we're trying to write some code to delete a given integer from a list. The straightforward solution looks like this:
/* delete node containing i from list pointed to by lp */ struct list *lp, *prevlp; for(lp = list; lp != NULL; lp = lp->next) { if(lp->item == i) { if(lp == list) list = lp->next; else prevlp->next = lp->next; break; } prevlp = lp; } }

This code works, but it has two blemishes. One is that it has to use an extra variable to keep track of the node one behind the one it's looking at, and the other is that it has to use an extra test to special-case the situation in which the node being deleted is at the head of the list. Both of these problems arise because the deletion of a node from the list involves modifying the previous pointer to point to the next node (that is, the node before the deleted node to point to the one following). But, depending on whether the node being deleted is the first node in the list or not, the pointer that needs modifying is either the pointer that points to the head of the list, or the next pointer in the previous node. To illustrate this, suppose that we have the list (1, 2, 3) and we're trying to delete the element 1. After we've found the element 1, lp points to its node, which just happens to be the same node that the main list pointer points to, as illustrated in (a) below:

To remove element 1 from the list, then, we must adjust the main list pointer so that it points to 2's node, the new head of the list (as shown in (b)). If we were trying to delete node 2, on the other hand (as illustrated in (c) above), we'd have to adjust node 1's next pointer to point to 3. The prevlp pointer keeps track of the previous node we were looking at, since (at other than the first node in the list) that's the node whose next pointer will need adjusting. (Notice that if we were to delete node 3, we would copy its next pointer over to 2, but since 3's next pointer is the null pointer, copying it to node 2 would make node 2 the end of the list, as desired.)

We can write another version of the list-deletion code, which is (in some ways, at least) much cleaner, by using a pointer to a pointer to a struct list. This pointer will point at the pointer which points at the node we're looking at; it will either point at the head pointer or at the next pointer of the node we looked at last time. Since this pointer points at the pointer that points at the node we're looking at (got that?), it points at the pointer which we need to modify if the node we're looking at is the node we're deleting. Let's see how the code looks:
struct list **lpp; for(lpp = &list; *lpp != NULL; lpp = &(*lpp)->next) { if((*lpp)->item == i) { *lpp = (*lpp)->next; break; } } }

That single line


*lpp = (*lpp)->next;

updates the correct pointer, to splice the node it refers to out of the list, regardless of whether the pointer being updated is the head pointer or one of the next pointers. (Of course, the payoff is not absolute, because the use of a pointer to a pointer to a struct list leads to an algorithm which might not be nearly as obvious at first glance.) To illustrate the use of the pointer-to-pointer lpp graphically, here are two more figures illustrating the situation just before deleting node 1 (on the left) or node 2 (on the right).

In both cases, lpp points at a struct node pointer which points at the node to be deleted. In both cases, the pointer pointed to by lpp (that is, the pointer *lpp) is the pointer that needs to be updated. In both cases, the new pointer (the pointer that *lpp is to be updated to) is the next pointer of the node being deleted, which is always (*lpp)->next. One other aspect of the code deserves mention. The expression
(*lpp)->next describes the next pointer

of the struct node which is pointed to by *lpp, that is, which is pointed to by the pointer which is pointed to by lpp. The expression
lpp = &(*lpp)->next sets lpp to point to the next field of the struct list pointed to by *lpp. In both cases, parentheses around *lpp are needed because the precedence of * is lower than ->.

the

As a second, related example, here is a piece of code for inserting a new node into a list, in its proper order. This code uses a pointer-to-pointer-to-struct list for the same reason, namely, so that it doesn't have to worry about treating the beginning of the list specially.
/* insert node newlp into list */ struct list **lpp; for(lpp = &list; *lpp != NULL; lpp = &(*lpp)->next) { struct list *lp = *lpp; if(newlp->item < lp->item) { newlp->next = lp; *lpp = newlp; break; } } }

2. #include <stdio.h>
#include <conio.h> void main() { int x=2,y=5; int *ptr; int **sptr; int ***ssptr; ptr = &x; // address of x *ptr = 0; sptr = &ptr; ssptr = & sptr; printf(" address is ip = %u %u %u",ptr,sptr,ssptr); _getch(); } III. Relationship between an array name and pointer 1. 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 != '\0', 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"); strlen(array); strlen(ptr); all work. As formal parameters in a function definition, char s[]; and char *s; are equivalent; we prefer the latter because it says more explicitly that the variable is a pointer. When an array name is passed to a function, the function can at its convenience believe that it has been handed either an array or a pointer, and manipulate it accordingly. It can even use both notations if it seems appropriate and clear. It is possible to pass part of an array to a function, by passing a pointer to the beginning of the subarray. For example, if a is an array, /* string constant */ /* char array[100]; */ /* char *ptr; */

f(&a[2]) and f(a+2) both pass to the function f the address of the subarray that starts at a[2]. Within f, the parameter declaration can read f(int arr[]) { ... } or f(int *arr) { ... } So as far as f is concerned, the fact that the parameter refers to part of a larger array is of no consequence. If one is sure that the elements exist, it is also possible to index backwards in an array; p[-1], p[2], and so on are syntactically legal, and refer to the elements that immediately precede p[0]. Of course, it is illegal to refer to objects that are not within the array bounds. 2. Pointers, of course, can be "pointed at" any type of data object, including arrays. While that was evident when we discussed program 3.1, it is important to expand on how we do this when it comes to multi-dimensional arrays. To review, in Chapter 2 we stated that given an array of integers we could point an integer pointer at that array using:
int *ptr; ptr = &my_array[0]; /* point our pointer at the first integer in our array */

As we stated there, the type of the pointer variable must match the type of the first element of the array. In addition, we can use a pointer as a formal parameter of a function which is designed to manipulate an array. e.g. Given:
int array[3] = {'1', '5', '7'}; void a_func(int *p);

Some programmers might prefer to write the function prototype as:


void a_func(int p[]);

which would tend to inform others who might use this function that the function is designed to manipulate the elements of an array. Of course, in either case, what actually gets passed is the value of a pointer to the first element of the array, independent of which notation is used in the function prototype or definition. Note that if the array notation is used, there is no need to pass the actual dimension of the array since we

are not passing the whole array, only the address to the first element. We now turn to the problem of the 2 dimensional array. As stated in the last chapter, C interprets a 2 dimensional array as an array of one dimensional arrays. That being the case, the first element of a 2 dimensional array of integers is a one dimensional array of integers. And a pointer to a two dimensional array of integers must be a pointer to that data type. One way of accomplishing this is through the use of the keyword "typedef". typedef assigns a new name to a specified data type. For example:
typedef unsigned char byte;

causes the name byte to mean type unsigned char. Hence


byte b[10];

would be an array of unsigned characters. Note that in the typedef declaration, the word byte has replaced that which would normally be the name of our unsigned char. That is, the rule for using typedef is that the new name for the data type is the name used in the definition of the data type. Thus in:
typedef int Array[10];

Array becomes a data type for an array of 10 integers. i.e. Array my_arr; declares my_arr as an array of 10 integers and Array arr2d[5]; makes arr2d an array of 5 arrays of 10 integers each. Also note that Array *p1d; makes p1d a pointer to an array of 10 integers. Because *p1d points to the same type as arr2d, assigning the address of the two dimensional array arr2d to p1d, the pointer to a one dimensional array of 10 integers is acceptable. i.e. p1d = &arr2d[0]; or p1d = arr2d; are both correct. Since the data type we use for our pointer is an array of 10 integers we would expect that incrementing p1d by 1 would change its value by 10*sizeof(int), which it does. That is, sizeof(*p1d) is 20. You can prove this to yourself by writing and running a simple short program. Now, while using typedef makes things clearer for the reader and easier on the programmer, it is not really necessary. What we need is a way of declaring a pointer like p1d without the need of the typedef keyword. It turns out that this can be done and that
int (*p1d)[10];

is the proper declaration, i.e. p1d here is a pointer to an array of 10 integers just as it was under the declaration using the Array type. Note that this is different from
int *p1d[10];

which would make p1d the name of an array of 10 pointers to type int.

3.

CHAPTER 2: Pointer types and Arrays

Okay, let's move on. Let us consider why we need to identify the type of variable that a pointer points to, as in:
int *ptr;

One reason for doing this is so that later, once ptr "points to" something, if we write:
*ptr = 2;

the compiler will know how many bytes to copy into that memory location pointed to by ptr. If ptr was declared as pointing to an integer, 4 bytes would be copied. Similarly for floats and doubles the appropriate number will be copied. But, defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code. For example, consider a block in memory consisting if ten integers in a row. That is, 40 bytes of memory are set aside to hold 10 integers. Now, let's say we point our integer pointer ptr at the first of these integers. Furthermore lets say that integer is located at memory location 100 (decimal). What happens when we write:
ptr + 1;

Because the compiler "knows" this is a pointer (i.e. its value is an address) and that it points to an integer (its current address, 100, is the address of an integer), it adds 4 to ptr instead of 1, so the pointer "points to" the next integer, at memory location 104. Similarly, were the ptr declared as a pointer to a short, it would add 2 to it instead of 1. The same goes for other data types such as floats, doubles, or even user defined data types such as structures. This is obviously not the same kind of "addition" that we normally think of. In C it is referred to as addition using "pointer arithmetic", a term which we will come back to later. Similarly, since ++ptr and ptr++ are both equivalent to ptr + 1 (though the point in the program when ptr is incremented may be different), incrementing a pointer using the unary ++ operator, either pre- or post-, increments the address it stores by the amount sizeof(type) where "type" is the type of the object pointed to. (i.e. 4 for an integer). Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, this brings up an interesting relationship between arrays and pointers. Consider the following:
int my_array[] = {1,23,17,4,-5,100};

Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, i.e. using my_array[0] through my_array[5]. But, we could alternatively access them via a pointer as follows:
int *ptr; ptr = &my_array[0]; /* point our pointer at the first integer in our array */

And then we could print out our array either using the array notation or by dereferencing our pointer. The following code illustrates this:
----------Program 2.1 ----------------------------------6/13/97 */

/* Program 2.1 from PTRTUT10.HTM #include <stdio.h>

int my_array[] = {1,23,17,4,-5,100}; int *ptr; int main(void) { int i; ptr = &my_array[0]; array */ printf("\n\n"); for (i = 0; i < 6; i++) { printf("my_array[%d] = %d } return 0; } ",i,my_array[i]); /*<-- A */ /*<-- B */ printf("ptr + %d = %d\n",i, *(ptr + i)); /* point our pointer to the first element of the

Compile and run the above program and carefully note lines A and B and that the program prints out the same values in either case. Also observe how we dereferenced our pointer in line B, i.e. we first added i to it and then dereferenced the new pointer. Change line B to read:
printf("ptr + %d = %d\n",i, *ptr++);

and run it again... then change it to:


printf("ptr + %d = %d\n",i, *(++ptr));

and try once more. Each time try and predict the outcome and carefully look at the actual outcome. In C, the standard states that wherever we might use &var_name[0] we can replace that with var_name, thus in our code where we wrote:
ptr = &my_array[0];

we can write:

ptr = my_array;

to achieve the same result. This leads many texts to state that the name of an array is a pointer. I prefer to mentally think "the name of the array is the address of first element in the array". Many beginners (including myself when I was learning) have a tendency to become confused by thinking of it as a pointer. For example, while we can write
ptr = my_array;

we cannot write
my_array = ptr;

The reason is that while ptr is a variable, my_array is a constant. That is, the location at which the first element of my_array will be stored cannot be changed once my_array[] has been declared. Earlier when discussing the term "lvalue" I cited K&R-2 where it stated: "An object is a named region of storage; an lvalue is an expression referring to an object". This raises an interesting problem. Since my_array is a named region of storage, why is my_array in the above assignment statement not an lvalue? To resolve this problem, some refer to my_array as an "unmodifiable lvalue". Modify the example program above by changing
ptr = &my_array[0];

to
ptr = my_array;

and run it again to verify the results are identical. Now, let's delve a little further into the difference between the names ptr and my_array as used above. Some writers will refer to an array's name as a [i]constant pointer. What do we mean by that? Well, to understand the term "constant" in this sense, let's go back to our definition of the term "variable". When we declare a variable we set aside a spot in memory to hold the value of the appropriate type. Once that is done the name of the variable can be interpreted in one of two ways. When used on the left side of the assignment operator, the compiler interprets it as the memory location to which to move that value resulting from evaluation of the right side of the assignment operator. But, when used on the right side of the assignment operator, the name of a variable is interpreted to mean the contents stored at that memory address set aside to hold the value of that variable. With that in mind, let's now consider the simplest of constants, as in:
int i, k; i = 2;

Here, while i is a variable and then occupies space in the data portion of memory, 2 is a constant and, as such, instead of setting aside memory in the data segment, it is imbedded directly in the code segment of memory. That is, while writing something like k = i; tells the compiler to create code which at run time will look at memory location &i to determine the value to be moved to k, code created by i = 2; simply puts the 2 in the code and there is no referencing of the data segment. That is, both k and i are objects, but 2 is not an object. Similarly, in the above, since my_array is a constant, once the compiler establishes where the array itself is to be stored, it "knows" the address of my_array[0] and on seeing:
ptr = my_array;

it simply uses this address as a constant in the code segment and there is no referencing of the data segment beyond that. This might be a good place explain further the use of the (void *) expression used in Program 1.1 of Chapter 1. As we have seen we can have pointers of various types. So far we have discussed pointers to integers and pointers to characters. In coming chapters we will be learning about pointers to structures and even pointer to pointers. Also we have learned that on different systems the size of a pointer can vary. As it turns out it is also possible that the size of a pointer can vary depending on the data type of the object to which it points. Thus, as with integers where you can run into trouble attempting to assign a long integer to a variable of type short integer, you can run into trouble attempting to assign the values of pointers of various types to pointer variables of other types. To minimize this problem, C provides for a pointer of type void. We can declare such a pointer by writing:
void *vptr;

A void pointer is sort of a generic pointer. For example, while C will not permit the comparison of a pointer to type integer with a pointer to type character, for example, either of these can be compared to a void pointer. Of course, as with other variables, casts can be used to convert from one type of pointer to another under the proper circumstances. In Program 1.1. of Chapter 1 I cast the pointers to integers into void pointers to make them compatible with the %p conversion specification. In later chapters other casts will be made for reasons defined therein.

Well, that's a lot of technical stuff to digest and I don't expect a beginner to understand all of it on first reading. With time and experimentation you will want to come back and re-read the first 2 chapters. But for now, let's move on to the relationship between pointers, character arrays, and strings.

11111Can an array of pointers represent multidimensional array

Dynamically Allocating Multidimensional Arrays


We've seen that it's straightforward to call malloc to allocate a block of memory which can simulate an array, but with a size which we get to pick at run-time. Can we do the same sort of thing to simulate multidimensional arrays? We can, but we'll end up using pointers to pointers. If we don't know how many columns the array will have, we'll clearly allocate memory for each row (as many columns wide as we like) by calling malloc, and each row will therefore be represented by a pointer. How will we keep track of those pointers? There are, after all, many of them, one for each row. So we want to simulate an array of pointers, but we don't know how many rows there will be, either, so we'll have to simulate that array (of pointers) with another pointer, and this will be a pointer to a pointer. This is best illustrated with an example:
#include <stdlib.h> int **array; array = malloc(nrows * sizeof(int *)); if(array == NULL) { fprintf(stderr, "out of memory\n"); exit or return } for(i = 0; i < nrows; i++) { array[i] = malloc(ncolumns * sizeof(int)); if(array[i] == NULL) { fprintf(stderr, "out of memory\n"); exit or return } } array is a pointer-to-pointer-to-int: at the first level, it points to a block of pointers, one for each row. That first-level pointer is the first one we allocate; it has nrows elements, with each element big enough to hold a pointer-to-int, or int *. If we successfully allocate it, we then fill in the pointers (all nrows of them) with a pointer (also obtained from malloc) to ncolumns number of ints, the storage for that row of the array. If this isn't quite making sense, a picture

should make everything clear:

Once we've done this, we can (just as for the one-dimensional case) use array-like syntax to access our simulated multidimensional array. If we write we're pointer pointed to by array, and then for the j'th int pointed to by that inner pointer. (This is a pretty nice result: although some completely different machinery, involving two levels of pointer dereferencing, is going on behind the scenes, the simulated, dynamically-allocated two-dimensional ``array'' can still be accessed just as if it were an array of arrays, i.e. with the same pair of bracketed subscripts.) If a program uses simulated, dynamically allocated multidimensional arrays, it becomes possible to write ``heterogeneous'' functions which don't have to know (at compile time) how big the ``arrays'' are. In other words, one function can operate on ``arrays'' of various sizes and shapes. The function will look something like
func2(int **array, int nrows, int ncolumns) { } function does accept a pointer-to-pointer-to-int, on the assumption array[i][j] asking for the i'th

This that we'll only be calling it with simulated, dynamically allocated multidimensional arrays. (We must not call this function on arrays like the ``true'' multidimensional array a2 of the previous sections). The function also accepts the dimensions of the arrays as parameters, so that it will know how many ``rows'' and ``columns'' there are, so that it can iterate over them correctly. Here is a function which zeros out a pointer-to-pointer, two-dimensional ``array'':
void zeroit(int **array, int nrows, int ncolumns) { int i, j; for(i = 0; i < nrows; i++) { for(j = 0; j < ncolumns; j++) array[i][j] = 0; } }

Finally, when it comes time to free one of these dynamically allocated multidimensional ``arrays,'' we must remember to free each of the chunks of memory that we've allocated. (Just freeing the top-level pointer, array, wouldn't cut it; if we did, all the second-level pointers would be lost but not freed, and would waste memory.) Here's what the code might look like:
for(i = 0; i < nrows; i++) free(array[i]); free(array); 3. These are unfinished sections that I deemed to advanced to belong in the beginning portion (part 2) of

my book. They will be re-instated at later chapters. All of this content refers to the elusive concept of pointing to pointers and how it is used.

Returning Strings from Functions

Arrays returned from functions suffer the same fate as strings. You cannot use assignment to place the value of one string into another and likewise you cannot return an array. There are two ways around this: return a pointer (the address of the first element in the array, or character in the case of strings) or use an output parameter.

Multiple Dimensions

Arrays are not limited to flat, linear lists of values. They can be multi-dimensional. Think of a grid: it has cells that can be accessed by both row and column. A grid is two-dimensional because points within it are accessed by two indices (rows and columns). Arrays can have two, three, four, or any amount of dimensions you wish. For each dimension you will need one more index.

Authors Preference: Arrays beyond the third dimension are difficult to visualize and best avoided.

To declare a multidimensional array you must specify a size in square brackets for the size of each dimension:

int two_d[10][10];
The above declaration creates a two-dimensional array called two_d. To access a subscript in the above array you will need to specify both dimensional indices:

int i = 0, j = 0; two_d[i][j] = 0;
Two indices are required to access a single subscript. The number of elements, or subscripts, in a multidimensional array is calculated by multiplying the size of each dimension. The array two_d as declared earlier has one hundred (100) total subscripts because it has two (2) dimensions each with ten (10) elements.

In this example, the variable two_d might be thought of as an array of arrays; it is perfectly example to think of multidimensional arrays in this manner. The first dimension contains the second dimension. That is, specifying only the first index in two_d is like referring to a row that then itself contains ten (10) subscripts of its own. Thus, to position yourself in an exact spot you must specify the row and then the column of that row:

two_d[row][col];
It is important to remember that the left-most dimensions specified contain those to the right. That is, the row contains ten (10) columns, but a column does not contain ten (10) rows. The code two_d*row+ specifies a single row, thus becoming the same as a flat, one-dimensional array of ten (10) subscripts. You could then use this subscript array as you would any normal array:

To be done: example of passing each row of a md array to a function accepting a flat array (remember to include a flat array in the example to show the association between the two).

Blarg.

Multiplication Table and Analogies

The multiplication table is a perfect example of a multidimensional array. If you never learned it, the multiplication table is a two-dimensional grid of numbers that contains all the answers for multiplying the row number to the column number:

1 2 3 4 5

2 4 6 8

3 6 9

4 8

5 10

12 15

12 16 20

10 15 20 25

The table above only shows the results of multiplying any two (2) numbers in the range of one (1) to five (5). For example, to see the result of multiplying three (3) and two (2) you would simply place your finger where the left-most column is three (3) and the right-most column is two (2). The answer is six (6). This is much like accessing a multidimensional array where you are accessing the subscript at index 3,2 or *3+*2+. The following program creates a 5x5 multiplication table in a multidimensional array and calculates multiplication answers from it:

To be done: insert multiplication table program here.

Another analogy of a multidimensional array is a chess or checker board. The location of each piece is not determined by a single value, it is determined by its row and column. Paper is considered flat: two dimensional. That is, when you have your pencil on a piece of paper, its location is determined by its horizontal and vertical offsets from the sides of the page. Lastly, remember the hype surrounding 3D gaming. The world we live in requires at least three dimensions to represent: vertical, horizontal, and depth. When you think of a square, you think of width and height. When you think of a cube you think of width, height, and depth: three dimensions.

It is my hope that I have instilled the essence of what multidimensional arrays are. They are blocks of data whose elements are located by multiple indices, one for each dimension.

Initializing Multiple Dimensions

A multidimensional array can be initialized in two ways. The first is by specifying the values of each dimension separately and the second is to initialize all the subscripts with a linear list of values.

Authors Preference: Initializing a multidimensional array with a linear list of values can confuse the fact that the array has more than one dimension. I usually avoid that method, but I wont pretend it doesnt exist either.

Remember how you initialize a flat array:

int arr[5] = { 0, 1, 2, 3, 4 };
A multidimensional array can be initialized in the same way, however you must initialize each dimension correctly. A dimension that simply contains another array must be initialized as an array of arrays whereas the right-most dimension actually has the values. Basically, for a two-dimensional array you will have the normal flat array initialization repeated a number of times equivalent to the size of the first dimension.

int mdarr[5][4];
The array above, mdarr, will have five (5) normal initializations. Each of those initializations will contain four (4) values. To initialize the entire array to zeroes:

int mdarr[5][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
Notice we have five (5) series of , 0, 0, 0, 0 -. Each one of those initializes a single row of mdarr to all zeroes. Every flat initialization is followed by a comma, and the whole thing is contained in a set of curly braces. Notice that each individual list of values is not followed by a semi-colon. A semi-colon terminates a statement, thus it comes at extreme end of the entire block. Where semi-colons would be there are now commas. But there are only commas between each list of values, there is not one after the last list.

Authors Opinion: Initializing multidimensional arrays can be truly confusing. At two dimensions it can be grasped easily with practice. At three dimensions, visualizing what you are doing becomes hideously difficult and at four and beyond can get lost in an alien world.

Pointer Pointer Arrays

Pointers have a unique relationship with arrays: they can represent them completely. This is true with multidimensional arrays as well as flat ones. Using a single index with a two-dimensional array specifies a single row of values. That is, the value of that subscript is a flat array. This flat array can be stored in a pointer:

int stuff[5][5]; int *p = stuff[0];


The above code causes p to point to the first value of the first row of stuff. The pointer p can now be used as if it was a flat array:

p[0] = 7; cout << p[0] << endl;


And pointer arithmetic can also be applied to it to move through the row:

p++; *p = 8; p++; *p = 9;
This has been seen before. The concept of a pointer to a list of values is interesting, but what about a pointer to a pointer of a list of values? A pointer pointer is just that. It is basically the pointer version of a two dimensional array and can be declared by preceding the pointer name with an additional asterisk:

int **pp;
A pointer pointer is practically equivalent to a two-dimensional array and the two can associated with one another:

pp = stuff; pp[0][0] = 10;


The pointer pointer can be used as if it was a two-dimensional array. It can also be used as a flat array if it is dereferenced:

(*pp)[0] = 10;
A pointer pointer can take the place of a multidimensional array in function parameters as well and it is easier to pass than an array. A pointer pointer is identical to a normal pointer: its value is a memory address. However, the value of a pointer pointer is the address of another pointer variable. The address contained by that pointer variable is the location of an actual value.

To be continued

Functions and Multidimensional Arrays

When I was first writing this chapter I got an email from someone who was trying to pass a multidimensional array to a function that accepted one. His compiler was giving him strange errors; as it did me when I tried it for myself. The reason was that multidimensional arrays must be treated uniquely when used as parameters to functions. The following is not valid:

void takemulti(int arr[][]);


It looks like it validly takes a two-dimensional array parameter called arr. Im sure theres a reason for it, but this is not technically legal. There are a few ways to remedy the problem.

void takemulti(int arr[][10]); void takemulti(int *arr[]); void takemulti(int **arr);

S-ar putea să vă placă și