POINTERS

Pointers
A pointer is a programming language object whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. For high-level programming languages, pointers effectively take the place of general purpose registers in low-level languages such as assembly language or machine code, but may be in available memory. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. A pointer is a simple, more concrete implementation of the more abstract reference data type. Several languages support some type of pointer, although some have more restrictions on their use than others.
Pointers to data significantly improve performance for repetitive operations such as traversing strings, lookup tables, control tables and tree structures. In particular, it is often much cheaper in time and space to copy and dereference pointers than it is to copy and access the data to which the pointers point.
Pointers are also used to hold the addresses of entry points for called subroutines in procedural programming and for run-time linking to dynamic link libraries (DLLs). In object-oriented programming, pointers to functions are used for binding methods, often using what are called virtual method tables.
Reference operator (&)
The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example:
myvar = 25;
foo = &myvar;
bar = myvar;

Dereference operator (*)
An interesting property of pointers is that they can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*). The operator itself can be read as "value pointed to by".
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
baz = *foo;

It is important to clearly differentiate that foo refers to the value 1776, while *foo (with an asterisk * preceding the identifier) refers to the value stored at address 1776, which in this case is 25. Notice the difference of including or not including the dereference operator (I have added an explanatory comment of how each of these two expressions could be read):
The reference and dereference operators are thus complementary:
& is the reference operator, and can be read as "address of"
* is the dereference operator, and can be read as "value pointed to by"
Pointers and string literals(*)
As pointed earlier, string literals are arrays containing null-terminated character sequences. In earlier sections, string literals have been used to be directly inserted into cout, to initialize strings and to initialize arrays of characters. But they can also be accessed directly. String literals are arrays of the proper array type to contain all its characters plus the terminating null-character, with each of the elements being of type const char (as literals, they can never be modified). For example:
const char * foo = "hello";
This declares an array with the literal representation for "hello", and then a pointer to its first element is assigned to foo. If we imagine that "hello" is stored at the memory locations that start at address 1702, we can represent the previous declaration as:
#include < iostream.h >
int addition (int a, int b)
{
return (a+b);
}
int subtraction (int a, int b)
{
return (a-b);
}
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;
m = operation (7, 5, addition);
n = operation (20, m, minus);
cout < return 0;
}
New Features in C++
Cast
In C, if you want to cast an int to a long int, for example, you'd use
int i=0;
long l = (long) i;
In C++, you can use a function-like call to make the cast.
long l = long(i);
Flexible Declarations
In C, all var declarations within a scope occur at the beginning of that scope. Thus, all global declartions must appear before any functions, and any local declarations must be made before any executable statements.
C++, on the other hand, allows you to mix data declarations with functions and executable statements. E.g. In C,
void makeit(void)
{
float i;
char *cp;
/* imagine 2000 lines of code here */
/* allocate 100 bytes for cp */
cp = malloc(100); /* 1st use of cp */
for (i=0; i<100; ++i) /* 1st use of i */
{
printf("%d",i)
}
printf("%d",i)
}
In C++,
void makeit(void)
{
// 2000 lines of code
char *cp = new char[100];
for (int i=1; i<10; i++)
{
cout< }
}
'struct' and 'union' Tags
In C, we would have this segment:
typedef struct
{
int a;
float b;
}
foo;
foo f;
In C++,
struct foo {int a; float b;
}
foo f;
Const
In ANSI C, it also supports 'const', but C++'s 'const' is more flexible than C's. In both C and C++, a value declared as 'const' is inviolate; it may not be modified by any part of the program in any way. The most common use of 'const' values in C is to replace '#define' literal constants
#define MAX_CUSTOMERS 10
const int MAX_CUSTOMERS = 10;
In C++, you can do something like
const int ArraySize = 100;
int Array[ArraySize];
The :: Operator
:: is called the scope resolution operator and is used to access an item hidden in the current scope.
'new' and 'delete'
In C, all dynamic mem allocation is handled via library calls, such as 'malloc' and 'free'.
void func(void)
{
int *i;
i = (int *)malloc(sizeof(int));
*i = 10;
printf("%d", *i);
free(i);
}
In C++, there are new ways of dynamically allocating mem using operators called 'new' and 'delete', where 'new' replaces 'malloc' and 'delete' replaces 'free' in C. We could rewrite the above function as the following:
void func()
{
int *i = new int;
*i = 10;
cout << *i;
delete i;
}
References
When you write a function to swap two integers, you have to pass the two integers into the function by reference: void swapint(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
C++ supports a special type of identifier known as 'reference' &. It makes changing the parameter values in a function reletively painless. The above function can be rewritten in C++ as follows:
void swapint(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
The this pointer
Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.
Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.A static member function does not have a this pointer.
The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x. You cannot declare the this pointer or make assignments to it.
The type of the this pointer for a member function of a class type X, is X* const. If the member function is declared with the const qualifier, the type of the this pointer for that member function for class X, is const X* const.
A const this pointer can by used only with const member functions. Data members of the class will be constant within that function. The function is still able to change the value, but requires a const_cast to do so.
#include < iostream >
using namespace std;
class Box
{
public:
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume()
{
return length * breadth * height;
}
int compare(Box box)
{
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2))
{
cout << "Box2 is smaller than Box1" < }
else
{
cout << "Box2 is equal to or larger than Box1" < }
return 0;
}
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1


Free Web Hosting