Pointer Data Type and Pointer Variables Pointer variable: content is a memory address There is no name associated with the pointer data type in C++. Declaring Pointer Variables Syntax: Data-type *identifier Examples: int *p; char *ch; These statements are equivalent: int *p; int* p; int * p; Dr. Hussein H. Owaied Business Networking and Systems Management Philadelphia University Amman-Jordan
Declaring Pointer Variables In the statement: int* p, q; only p is the pointer variable, not q; here q is an int variable To avoid confusion, attach the character * to the variable name: int *p, q; int *p, *q; Address of Operator (&) The ampersand, &, is called the address of operator The address of operator is a unary operator that returns the address of its operand. Dereferencing Operator (*) When used as a unary operator, * is the dereferencing operator or indirection operator Refers to object to which its operand points
Application of Operator (&) and (*) When used as a unary operator, * is the dereferencing operator or indirection operator Refers to object to which its operand points Example 1: Int x=5; Int *p; P=&x; //store the address of x in p To print the value of x, using p: Cout << *p << endl; //will print 5 To store a value in x, using p: *p =25; //Now x=25
//Program 1 Arrays and Pointers //Manipulation of an array through a pointer char cstr[28] = "Welcome to wherever you are."; #include<iostream.h> void main() { char *cptr; cout<<"the string is: "<<cstr<<endl; cptr = cstr; //cptr is pointing to wherever cstr is pointing to cout<<"the string is: "<<cptr<<endl; } The output of Program 1 is: The string is Welcome to wherever you are. The string is Welcome to wherever you are.
Explanation of using pointer Although Program 1 gives an impression that arrays and pointers are the same, there is a subtle difference between the two. Both cstr and cptr are pointers to char data type. However, cstr as a pointer is constant, which means it always points to the first element in the array of 28 characters. Therefore, the value of cstr cannot be changed. But cptr as a pointer is dynamic. It can be made to any address in memory. To understand the concept better, consider the following example:
Continue Explanation of using pointer //Program 2 //This program demonstrates the dynamic nature of pointers #include<iostream.h> char cstr[] = "Mad dogs go to heaven"; void main() { char *cptr, cvar = 'A'; cptr = cstr; //cptr and cstr both point to the same location, that is the first element of the array cstr cout<<cptr<<endl; cptr = &cvar; //Now cptr is pointing to the variable cvar cout<<*cptr; } The output of Program 2 is: Mad dogs go to heaven A
Dynamic Memory Allocation The main drawback of arrays is that the size of the arrays is fixed and it needs to be specified at compile time. Memory can be deallocated and reallocated dynamically by using the new and the delete operators. new Operator Pointers provide the necessary support for C++'s powerful dynamic memory allocation system. Dynamic allocation is the means by which a program can obtain memory while it is running. The syntax for the new operator is: <variable> = new <type>; where <variable> is a pointer variable and <type> can be char, int, float or any user defined data type. The type of variable mentioned on the left hand side and the type mentioned on the right hand side should match. Consider the following examples:
Examples for new and delete operators char *cptr; int *iptr; cptr = new char[10]; // This declaration allocates ten bytes to the pointer cptr. iptr = new int; // This declaration allocates four bytes of memory // and assigns the starting address to iptr. The syntax of the new operator can also be modified to allocate memory of varying requirements. For example, char *cprt; //allocates five bytes of memory and assigns the starting address to cptr. cptr = new char[5]; delete Operator The delete operator is used to release the memory, which was allocated, using the new operator. The syntax of the delete operator is: delete <variable>; where <variable> is a pointer variable. Consider the following example: char *ps; ps = new char[10]; delete ps; //The above code releases the allocated memory to the pointer ps
Modular Approach Functions are the building blocks of C++ programs. A function groups a number of program statements into a single unit. This unit can be invoked from other parts of the program. Therefore we need a function. A group of functions together form a large entity called a module. There are may reasons for using function such as: 1. When programs become large, a single list of instruction becomes difficult to understand. 2. A function has a cleared defined purpose and has an interface to other functions in the program. 3. Using functions is to reduce the size of the program. Any sequence of instructions that is repeated in a program can be grouped together to form a function. The function code is stored in only one place in the memory even though the function may be executed a repeated number of times.
How we deal with Function There are three concepts should be considered: 1. Function Prototype or Function Declaration 2. Function Definition 3. Function Call Statement Function Prototype or Function Declaration The syntax of function prototype Function Declaration is: Data-type function-name(formal-parameters-list); Formal-parameters-list means the formal-data-types of the function parameters. Formal-parameters-list >= nill Examples: int sum(int,int); char str(char, char); int add();
Function Definition After writing function prototype must define it. The syntax of definition a function is: Data-type function-name(formal-1 var1, formal-2 var2, formal-3 var3,..) { Function Body } Example: int sum(int x, int y) { double z; cin >>x; cin >>y; z=x+y; cout <<z; }
Function Call Statement The syntax of function call statement is: Function-name(actual-parameters-list); Example : Sum(5,9); //this is function call statement Where 5,9 are actual parameters Note: function call without data-type of function