Sei sulla pagina 1di 17

C++ Programming Structures (Part V)

QF002/6/1

UNIT 6

C++ Programming Structures (Part V)

OBJECTIVES

General Objective

To understand and apply the fundamental concept of pointers in C++ programming.

Specific Objective

At the end of the unit you should be able to :

 Describe the declarations of pointer.  Explain how to declare and initialize pointer.  Use pointer in program.  Write and design a simple pointer program.

C++ Programming Structures (Part V)

QF002/6/2

INPUT

6.0

Introduction

Pointer is a variable whose value is used to point to another variable. It is able to assign different values to a pointer variable and the value contained by a pointer must be an address that indicates the location of another variable in the memory. That is why a pointer is also called an address variable

OHHHHH.

6.1 Declaring Pointer

 A pointer is a variable which means that a pointer has a left value and a right value. The left and right values are addressed. 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.

C++ Programming Structures (Part V)

QF002/6/3

 The general form of a pointer declaration is that data type    *pointer name;

The data type specifies the type of data to which the pointer points. The pointer name is the name of the pointer variable The asterik (*) indicates the variable as a pointer.

The following shows the different types of pointer: char *ptr_c ; int *ptr_int; ; float *ptr_flt ; // declare a pointer to a character // declare a pointer to a integer // declare a pointer to a float

Can a null pointer point to valid data ? No A Null pointer cannot point to valid data. This is so because the value contained by a null pointer is 0.

C++ Programming Structures (Part V)

QF002/6/4

6.2 Array Of Pointer And Pointer To Array

 The elements of an array may be pointers. Here is an array of 4 pointers to type double:
double * p [ 4]

 Their elements can be allocated like any other pointers:


p[3] = new double ( 5.1234);

note

A pointer is a special type of variable that contains the memory address of another variables.

 We can visualize this array like this:

p 0 1 2 3
5.1234

Pointers are used to hold the address of objects on the free store.

Why are pointers so important?

C++ Programming Structures (Part V)

QF002/6/5

6.3 Pointer To Pointer

 A pointer may point to another pointer. For example:


char x =t; char *yx = &x; char **yyx = &yx; char ***yyyx = & yyx; ***yyyx = w;

Change the value of c to w

 We can visualize these variable as shown below.

yyyx yyx yx x

t
Figure 6.1 A Model Showing Visualizing Pointer To Pointer

What is a pointer? A pointer is a variable that holds the address of another variable.

C++ Programming Structures (Part V)

QF002/6/6

6.4 Pointer To Functions

 Like an array name, a function name is actua lly a constant pointer. We can think of its value as the address of the code that implements the function.  A pointer to a function is simply a pointer whose value is the address of the function name. Since that name itself is a pointer, a pointer to a function is just a pointer to a constant pointer. For example :

int x (int) int (* yx) (int); yx =&x;

Declares the function x Declares the function pointer yx Assign the address of x to yx

 We can visualize the function pointer as shown in figure below.

yx x
Int x (int n) { ::::::::::::::::::::::::::::: }

Figure 6.2: A Model Showing Visualizing Pointer To Functions

C++ Programming Structures (Part V)

QF002/6/7

 The value of function pointer is that they allow us to define function of functions. This is done by passing a function pointer as a parameter to another function.

6.5 Introduction to Module Programs

 Is a process of developing programs in terms of subprograms or module.  A module is a major task of a program or process. Source file s are also called modules. Data declarations and functions prototypes can be placed in header files. This will enable any changes to the declarations or the prototypes into header files to be automatically signified to all source files including the header files.  Modular programming is to break down the design of a program into individual components that can be programmed and tested independently. It is a requirement for effective development and maintenance of large programs and projects.  Modular programming has evolved into object -oriented

programming, which provides formal rules for developing self contained software modules.

note

The pointer provides an indirect way to get the value held at that address.

C++ Programming Structures (Part V)

QF002/6/8

6.6 Modular with functions.

 Programmer can make the functions available to any program if programmers separate the function declarations into a header file and put the function definition in the source file.  Figure 6.4 below shows the example of the header file of module1.h
1. // module : module1.h 2. //purpose : interface for module1.cpp 3. #ifndef _ MODULE1_H 4. #define _MODULE1_H 5. #inculde <stdlib.h> 6. #include <ctype.h> 7. #include <string.h> 8. // function prototype 9. extern char * LCase (char * str) 10. extern char * UCase (char * str) 11. # endif // _MODULE1_H

Preprocessor directives for single inclusion.

End of single inclusion

Figure 6.3: An Example Of Modular Statement

Note:
In line 3, the preprocessor directive #ifndef asks the question, has the preprocessor identifier _MODULE1_H been defined ? If not, line 4 defines the identifier and everything in the file until the # endif directive is included. If the symbol _MODULE1_H has already been defined, file inclusion jumps from the #ifdef directive to the #endif

C++ Programming Structures (Part V)

QF002/6/9

 Figure 6.4 below shows the example of the source code ( .cpp)
#include module1.h char *Ucase ( char *str) { int len = strlen (str) for (int i = 0; I < len ; i++) str[i] = toupper (str [i]); return str; } char * Lcase (char *str) { int len = strlen (str); for (int i=0; i< len; i++) str[i] = tolower(str [i]); return str; } The function interface file

These functions are isolated because they are in a file separated from any other functions.

Figure 6.4: An Example Of The Source Code


The address stored in the pointer is the address of another variable. The value stored at that address is any value stored in any variable. The indirection operator (*) returns the value stored at the address, which itself is stored in the pointer.

What is the difference between the address stored in a pointer and the value at that address?

note

Modular design a method of programming that separates program components into well-defined modules that can be used to hide data and promote code reuse.

C++ Programming Structures (Part V)

QF002/6/10

 Figure 6.5 below shows the simple drive program to test the module1 function.
1. #include <iostream.h> 2. #include module1.h 3. void main() 4. 5. 6. 7. { cout << this ia a test \n; cout << Ucase ( this is a test of Ucase \n) cout << Lcase ( THIS IS A T EST OF LCASE);

8. }

Figure 6.5: A Simple Drive Program

Note:
After including the module header file in line 2, the module1.cpp module knows about Lcase () and Ucase and can use the functions freely on lines 5 through 7. This approach to programming is called module design and it keeps data in the modules where it belongs.

C++ Programming Structures (Part V)

QF002/6/11

Activity 6

Test your comprehension before continuing to the next input. Check your answers on the next page.

6.1.

Explain the difference between the following two uses of the reference operator &: int& r =n; p = &n;

6.2.

Explain the difference between the following two uses of the reference operator *: int* a =b; c = *b;

6.3.

What is wrong with the following code ? int& j = 33;

6.4.

What is wrong with the following code ? Int * j = &33;

6.5.

What is wrong with the following code ? char j = a; char k = &j;

C++ Programming Structures (Part V)

QF002/6/12

Feedback 6
Make sure you have tried to answer all the questions given. You can check your answers with the answers below.

6.1.

The declaration int& r = n; declares r to be a reference (alias) for the int variable n. the assignment p = &n; assigns the address of n to the pointer p.

6.2.

The declaration int *a = b; declares a to be a pointer (memory address) pointing to the same int to which c points. The assignment c = * b; assigns to n the int to which b points.

6.3.

You cannot have a reference to a constant: its address is not accessible.

6.4.

The reference operator & cannot be applied to a constant.

6.5.

The variable k has type char, while the expression &j has type pointer to char. To initialize k to &j, k would have to be declared as type char *.

C++ Programming Structures (Part V)

QF002/6/13

Key Facts

 


A pointer is a special type of variable that contains the memory address of another variables. A Null pointer cannot point to valid data. This is so because the value contained by a null pointer is 0. A pointer to a function is simply a pointer whose value is the address of the function name Module Programs is a process of developing programs in terms of subprograms or module. Modular design a method of programming that separates program components into well-defined modules that can be used to hide data and promote code reuse .

C++ Programming Structures (Part V)

QF002/6/14

Self-Assessment

You are approaching success, please answer the questions below. If you have any problems, please discuss it with your lecturer. Wish you good luck and all the best.
Question 6-1

a. Determine the value of each of the indicated variable after the following code executes. Assume that integers occupy 4 bytes and that x is stored in memory starting at byte 0x3fffd00 int x = 44; int *p = &x; int & r = x; int n = (*p) ++; int *q = p -1; r = * (-- p) +1; ++ *q; i. x ii. n iii. &x iv. *p v. r vi. *q

b. What is wrong with the following code? float x = 7.12345; float *p = &x;

C++ Programming Structures (Part V)

QF002/6/15

short d = 69; short *q = &d; p = q; c. What is wrong with the following code ? int * x = new int; int *y = new int; cout << x = << x << , x + y = << x + y << endl; d. Explain why the following three conditions are true for array x and int y: x[y] = = * (x +y); * ( x + y) = = y [x]; x[y] = = y[x]; e. Explain the difference between the following two declarations: double * xj( ); double (* j) ( );

f. Write a declaration for each of the following: i. ii. iii. iv. v. vi. vii. viii. An array of 8 floats; An array of 8 pointers to a float; A pointer to an array of 8 floats; A pointer to an array of 8 pointers to a float; A function that returns a pointer to a float; A function that returns a float; A pointer to a function that returns a float; A pointer to a function that returns a pointer to a float;

C++ Programming Structures (Part V)

QF002/6/16

Feedback On Self-Assessment

Make sure you have tried to answer all the questions given. You can check your answers with the answers below

Answer

6-1

a. i. ii. iii. iv. v. vi. x = 46 n = 44 &x = 0x3fffd00 *p =46 *r =46 *q =46

b. It is an error to add two pointers.

c. The value x[y] returned by the subscripting operator [ ] is the value stored at the address computed from the expression x + y. In that expression, x is a pointer to its base type T and y is an int, so the offset y *sizeof (T) is added to the address x. The same evaluation would be made from the expression y + x which is what would be used for y[x].

d. The declaration double *j ( ); declares j to be a function that returns a pointer to double, the declaration double ( * j) ( ); declares *j to be a pointer to a function that returns a double int.

C++ Programming Structures (Part V)

QF002/6/17

e. i. float a[8]; ii. float * a[8]; iii. float (*a)[8]; iv. float * (* a)[8]; v. float f ( ); vi. float * f ( ); vii. float (* f) ( ); viii. float * (* f) ( );

CONGRATULATIONS May success be with you always..

Potrebbero piacerti anche