Presentation is loading. Please wait.

Presentation is loading. Please wait.

void Pointers Lesson xx

Similar presentations


Presentation on theme: "void Pointers Lesson xx"— Presentation transcript:

1 void Pointers Lesson xx
 In this presentation, we’ll talk about void pointers.

2 Objectives Quick pointer review What is a void pointer
Manipulating void pointers First, we’ll do a quick review of pointers and then we’ll relate them to void pointers. Finally, we’ll write a short program that demonstrates how to manipulate void pointers.

3 Pointer Review int * pi; char * pc; double * pd; time * pt;
Let’s review the different types of pointers. Basically, you can have a pointer to any data type. In example # 1, int *pi; pi is a pointer to an integer. In example #2, char *pc; pc is a pointer to a char. These are called pointers to basic data types. We can also have a pointer to user defined data types. Example #4, time *pt; declares pt to be a pointer to a structure of the form time.

4 void Pointer void * pv; This is how you declare a void pointer: void * pv; It sounds like this is a pointer to nothing but what it really means is that pv can pointer to any data type. In the previous slide, we had: int * pi; and double *pd; pi can only point to an integer and pd can only point to a double. However, the void pointer pv can point to anything, a double, a long, a structure and etc.

5 Program to Manipulate void Pointers
#include <iostream> using std::cout; using std::endl; int main() { int x = 5; double y [10] = {1.3, 2.5, 3.7, 4.8}; void *ptr; ptr = &x; cout << "the value in x is " << * (int *) ptr << endl; ptr = y; cout << " the third number in the array is " << *(double *) ( (double *) ptr +2 ) << endl; return 0; } Here is the listing of the program that manipulates void pointers. In the following slides, we’ll discuss each line in detail.

6 Declarations and Initializations
#include <iostream> using std::cout; using std::endl; int main() { int x = 5; double y [10] = {1.3, 2.5, 3.7, 4.8}; void *ptr; ptr = &x; cout << "the value in x is " << * (int *) ptr << endl; ptr = y; cout << " the third number in the array is " << *(double *) ( (double *) ptr +2 ) << endl; return 0; } 5 1.3 2.5 3.7 . . . 0.0 x (56c) y[0] (7a1) y[1] (7a5) y[2] (7a9) y[9] The first couple of lines are the preprocessor directives. In main() we have declared x as an integer and initialized it to 5. In our drawing, x has the memory address 56c. The 2nd statement in red, sets up an array of doubles called y. We have also initialized the 1st few elements of the array. On the right side of the slide is a picture of array y.

7 Declaration of a void Pointer
#include <iostream> using std::cout; using std::endl; int main() { int x = 5; double y [10] = {1.3, 2.5, 3.7, 4.8}; void *ptr; ptr = &x; cout << "the value in x is " << * (int *) ptr << endl; ptr = y; cout << " the third number in the array is " << *(double *) ( (double *) ptr +2 ) << endl; return 0; } ptr 5 1.3 2.5 3.7 . . . 0.0 x (56c) y[0] (7a1) y[1] (7a5) y[2] (7a9) y[9] The statement: void *ptr; sets up a void pointer called ptr. At this point, ptr isn’t pointing to anything but, because it is a void pointer, it can point to x which is an integer or it can point to y[2] which is a double. void pointers can point to variables of any data type.

8 Initialize Pointer ptr
#include <iostream> using std::cout; using std::endl; int main() { int x = 5; double y [10] = {1.3, 2.5, 3.7, 4.8}; void *ptr; ptr = &x; cout << "the value in x is " << * (int *) ptr << endl; ptr = y; cout << " the third number in the array is " << *(double *) ( (double *) ptr +2 ) << endl; return 0; } ptr 56c 5 1.3 2.5 3.7 . . . 0.0 x (56c) y[0] (7a1) y[1] (7a5) y[2] (7a9) y[9] ptr = &x; initializes the pointer called ptr to the address of x. In our diagram, ptr contains 56c which is the address of x. Another way of saying this is: ptr is pointing to x. The cout statement in red prints out the contents of x indirectly through the void pointer ptr. It might be easier to understand this statement if we relate it to simple pointers 1st.

9 Simple pointer int x = 10; int * ptr = &x; 10 x (3ab) ptr 3ab
cout << *ptr; 10 3ab x (3ab) ptr In this small snippet of code, the variable x contains the # 10 and the pointer variable contains 3ab which is the address of x. In summary, ptr is pointing to x. In order to print out the contents of x indirectly through the pointer variable ptr, we write: cout << * ptr; which can be interpreted as: print out the contents of what is pointed to by ptr. * (pointer variable) refers to the contents of what is pointed to by pointer variable.

10 Indirection #include <iostream> using std::cout; using std::endl; int main() { int x = 5; double y [10] = {1.3, 2.5, 3.7, 4.8}; void *ptr; ptr = &x; cout << "the value in x is " << * (int *) ptr << endl; ptr = y; cout << " the third number in the array is " << *(double *) ( (double *) ptr +2 ) << endl; return 0; } ptr 56c 5 1.3 2.5 3.7 . . . 0.0 x (56c) y[0] (7a1) y[1] (7a5) y[2] (7a9) y[9] Going back to our program, you would think that writing: cout << *ptr; would print out the contents of x. Well, it turns out that with void pointers, you need to do an extra step which is called typecasting the pointer. You write: cout << * (int *) ptr; The (int*) is called typecasting. It tells the computer that ptr is pointing to an integer. Why do we have to type cast void pointers? It’s because they can point to any data type and the computer needs a little help via typecasting to know how many bytes to pick up from memory.

11 Comparison int * ptr; . . . cout << * ptr; void * ptr; . . .
Let’s see if we can simplify things for you. If ptr were declared as a pointer to an int as in the top box, you would write: cout << * ptr; in order to print out the contents of what is pointed to by ptr. However, if ptr were declared as a void pointer, you have to add the typecasting which is in red. void * ptr; . . . cout << * (int *) ptr;

12 Indirection #include <iostream> using std::cout; using std::endl; int main() { int x = 5; double y [10] = {1.3, 2.5, 3.7, 4.8}; void *ptr; ptr = &x; cout << "the value in x is " << * (int *) ptr << endl; ptr = y; cout << " the third number in the array is " << *(double *) ( (double *) ptr +2 ) << endl; return 0; } ptr 7a1 5 1.3 2.5 3.7 . . . 0.0 x (56c) y[0] (7a1) y[1] (7a5) y[2] (7a9) y[9] To show you that ptr can also point to a different data type, we have the statement: ptr = y; This makes ptr point to the 1st element of array y which is a double. The cout statement in red prints out the the 3rd element of the array which is y[2] and contains the # 3.7.

13 Analysis cout << * (double *) ( (double *) ptr +2 ) ; cast ptr
indirection cast ptr cout << * (double *) ( (double *) ptr +2 ) ; cast (ptr+2) Let’s analyze only at the important part of the cout statement. The 1st * is for indirection, the (double * ) in red is the typecasting for ptr. Since (ptr+2) is also an address, that is typecast with the (double*) in green. With the later version of the compiler, the typecast in green may be omitted. Even though void pointers can point to any data type and are versatile, you can see that the syntax is very difficult when you have to dereference them. We don’t use them a lot but there are times when you absolutely have no choice. An example of this situation is when you call the built-in qsort function which we will look at in another module.

14 Summary Quick pointer review What is a void pointer
Manipulating void pointers In this module, we did a quick review of pointers, talked about a void pointer and then wrote a program that showed how to manipulate a void pointer. We’ll look at some examples of how void pointers are used in another module.


Download ppt "void Pointers Lesson xx"

Similar presentations


Ads by Google