Professional Documents
Culture Documents
Session -VII
Agenda
Pointers
Arrays using Pointers
Enumeration
Typedef
Pointers
Pointer Variables
Pointer Assignments
As with any variable, you may use a pointer on the right-hand side
of an assignment statement to assign its value to another pointer.
#include iostream
using namespace std;
int main(void)
{
int x;
int *p1, *p2;
p1 = &x;
p2 = p1;
cout<< p2; /* print the address of x, not x's value! */
return 0;
}
Both p1 and p2 now point to x.
Pointer Arithmetic
There are only two arithmetic operations that you may use
on pointers:
1)addition and
2)subtraction.
Each time a pointer is incremented, it points to the memory
location of the next element of its base type.
Each time it is decremented, it points to the location of the
previous element.
Ex: int *p;
p++;
p--;
This approach ensures that a pointer is always pointing to
an appropriate element of its base type.
Pointer Arithmetic
int arr[4];
Arrays of Pointers
Multiple Indirection
You can have a pointer point to another pointer that points to the
target value. This situation is called multiple indirection, or
pointers to pointers.
A variable that is a pointer to a pointer must be declared as such.
You do this by placing an additional asterisk in front of the
variable name.
For example, the following declaration tells the compiler that
newbalance is a pointer to a pointer of type float:
float **newbalance;
Example
#include iostream
using namespace std;
int main(void)
{
int x, *p, **q;
x = 10;
p = &x;
q = &p;
Cout<<**q; /* print the value of x */
return 0;
Initializing Pointers
Pointers to Functions
Pointers to Functions
Pointers to Functions
Alternatively,
void Pointers
Typedef
You can create a new name for an existing type using typedef.
Following is the simple syntax to define a new type using
typedef:
typedef type newname;
For example, the following tells the compiler that feet is another
name for int:
typedef int feet;
Now, the following declaration is perfectly legal and creates an
integer variable called distance:
feet distance;
Now that feet has been defined, it can be used in another
typedef. For example,
typedef feet height;
tells the compiler to recognize height as another name for feet,
which is another name for int.
Enumeration
Enumeration
Enumeration
You can specify the value of one or more of the symbols by using
an initializer.
Do this by following the symbol with an equal sign and an integer
value.
Symbols that appear after initializers are assigned values greater
than the previous initialization value.
For example, the following code assigns the value of 100 to
quarter:
symbols
value
enum coin {
penny, nickel, dime,
quarter=100,half_dollar, dollar
};
Now, the values of these symbols are
penny
nickel
dime
quarter
100
half_dollar
101
dollar
102
Enumeration
Summary
In this session we have learned Pointers
Arrays using Pointers, Enumeration and Typedef
Assignment- 11,12&13
String
String
String
String
comparison
concatenate
copy
length