void Pointers Lesson xx

Slides:



Advertisements
Similar presentations
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Advertisements

CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
 Review structures  Program to demonstrate a structure containing a pointer.
CSC 107 – Programming For Science. The Week’s Goal.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Workin’ with Pointas An exercise in destroying your computer.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Intro to Pointers in C CSSE 332 Operating Systems
CS1010 Programming Methodology
CS1010 Programming Methodology
EGR 2261 Unit 11 Pointers and Dynamic Variables
Popping Items Off a Stack Using a Function Lesson xx
Pointers.
Lesson #6 Modular Programming and Functions.
Motivation and Overview
Two-Dimensional Arrays Lesson xx
Lesson #6 Modular Programming and Functions.
Pointers and Pointer-Based Strings
Student Book An Introduction
Pointers Psst… over there.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Lesson #6 Modular Programming and Functions.
Arrays & Functions Lesson xx
Structures Lesson xx In this module, we’ll introduce you to structures.
Variables with Memory Diagram
Chapter 2 Elementary Programming
Returning Structures Lesson xx
Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
File I/O with Records Lesson xx
Passing Structures Lesson xx
Popping Items Off a Stack Lesson xx
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Value returning Functions
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Pointer to Structures Lesson xx
Pointers Kingdom of Saudi Arabia
Functions Pass By Value Pass by Reference
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
5.1 Introduction Pointers Powerful, but difficult to master
Pointers Lecture 1 Thu, Jan 15, 2004.
Lesson #6 Modular Programming and Functions.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Overloading functions
C++ Pointers and Strings
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Java Programming Language
Creating and Using Pointer Variables in C++ By: Ed Brunjes
Pointers and dynamic objects
CISC181 Introduction to Computer Science Dr
Pointers and pointer applications
Miscellaneous Topics I
Pointers, Dynamic Data, and Reference Types
Introduction to Pointers
Introduction to Pointers
Presentation transcript:

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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;

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.

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.

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.